这就是我所做的。
create proc INITCAP(@string varchar(30)) as begin SET @string = UPPER(LEFT(@string,1)) + LOWER(RIGHT(@string, LEN(@string) -1)) end declare @lastname varchar set @lastname = exec INITCAP 'MILLER' declare @firstname varchar set @firstname = exec INITCAP 'StEvE' UPDATE Employee SET firstname = @firstname, lastname = @lastname WHERE empID = 7934
我不断收到错误:
消息156,级别15,状态1,过程INITCAP,第97行 关键字“ exec”附近的语法错误。 消息156,级别15,状态1,过程INITCAP,第100行 关键字“ exec”附近的语法错误。
我该怎么办?我希望该过程INITCAP像在Oracle中一样工作:返回一个名称,例如:“ Steve”,“ Miller”
INITCAP
解决方案1 (我不会使用此解决方案)
您可以这样使用OUTPUT参数:
create proc INITCAP(@string varchar(30) OUTPUT) as begin SET @string = UPPER(LEFT(@string,1)) + LOWER(SUBSTRING(@string, 2, 8000)) end go declare @lastname varchar set @lastname = 'MILLER' exec INITCAP @lastname OUTPUT declare @firstname varchar set @firstname = 'StEvE' exec INITCAP @firstname OUTPUT
解决方案2 :相反, 我将选择 创建一个内联函数,因此:
CREATE FUNCTION dbo.Capitalize1(@string varchar(30)) RETURNS TABLE AS RETURN SELECT UPPER(LEFT(@string,1)) + LOWER(SUBSTRING(@string, 2, 8000)) AS Result;
用法:
UPDATE e SET firstname = cap.Result FROM Employee e CROSS APPLY dbo.Capitalize1(e.firstname) cap;
解决方案3 :另一个选项可能是标量函数with schemabinding选项(出于性能原因):
with schemabinding
CREATE FUNCTION dbo.Capitalize2(@string varchar(30)) RETURNS VARCHAR(30) WITH SCHEMABINDING AS BEGIN RETURN UPPER(LEFT(@string,1)) + LOWER(SUBSTRING(@string, 2, 8000)); END;
UPDATE Employee SET firstname = dbo.Capitalize2(firstname);