存储过程是:
CREATE PROCEDURE CountUtily
@domain varchar(50),
@count int,
@totalCount int OUT
AS
BEGIN
SET NOCOUNT ON
SET @totalCount=0
IF (EXISTS (SELECT @totalCount = count
FROM FormFillerAuto2_DomainCount
WHERE domain = @domain))
BEGIN
SET @totalCount = @totalCount + @count
UPDATE FormFillerAuto2_DomainCount
SET count = @totalCount
WHERE domain = @domain
END
ELSE
BEGIN
INSERT INTO FormFillerAuto2_DomainCount (domain, count)
VALUES (@domain, @count)
END
END
错误:
' ='附近的语法不正确。关键字' ELSE'附近的语法不正确。
答案 0 :(得分:2)
您的Select @totalCount = count不会返回bool。尝试在if评估之前设置@totalCount,并在if
中评估计数CREATE PROCEDURE CountUtily
@domain varchar(50),
@count int,
@totalCount int OUT
AS BEGIN
SET NOCOUNT ON
SET @totalCount=0
SELECT @totalCount=count FROM FormFillerAuto2_DomainCount WHERE
IF (@totalCount > 0)
begin
SET @totalCount=@totalCount+@count
UPDATE FormFillerAuto2_DomainCount SET count=@totalCount WHERE domain=@domain
end
ELSE
begin
INSERT INTO FormFillerAuto2_DomainCount (domain, count) VALUES (@domain, @count)
end
end
答案 1 :(得分:1)
请参阅EXISTS
指定要测试行是否存在的子查询。它返回
restricted SELECT statement
如果子查询包含任何行,则会接受INTO
,但不允许使用alter PROCEDURE CountUtily @domain varchar(50), @count int, @totalCount int OUT AS BEGIN SET NOCOUNT ON SET @totalCount=0; IF (EXISTS (SELECT [count] FROM FormFillerAuto2_DomainCount WHERE domain=@domain)) begin SELECT @totalCount=[count] FROM FormFillerAuto2_DomainCount WHERE domain=@domain UPDATE FormFillerAuto2_DomainCount SET count=@totalCount WHERE domain=@domain end ELSE begin INSERT INTO FormFillerAuto2_DomainCount (domain, count) VALUES (@domain, @count) end end
关键字。此处的问题是您无法在Exists中设置值。
尝试
{{1}}
答案 2 :(得分:0)
我相信你缺少;
来标记声明的结尾。同样,count
是一个保留字,因此使用[]
来逃避它。您发布的程序可以修改为
CREATE PROCEDURE CountUtily(
@domain varchar(50),
@count int,
@totalCount int OUT)
AS BEGIN
SET NOCOUNT ON;
SET @totalCount=0;
SELECT @totalCount=[count] FROM FormFillerAuto2_DomainCount WHERE domain=@domain;
IF (@totalCount IS NOT NULL)
begin
SET @totalCount=@totalCount+@count;
UPDATE FormFillerAuto2_DomainCount SET [count]=@totalCount WHERE domain=@domain;
end
ELSE
INSERT INTO FormFillerAuto2_DomainCount (domain, [count]) VALUES (@domain, @count);
end
答案 3 :(得分:0)
你使用count和totalcount变量使这个查询过于复杂;所有你不需要的。
因此,当字段“domain”与参数@domain匹配时,您希望更新“FormFillerAuto2_DomainCount”的“count”字段;或者你想插入它,如果它不存在。
好的,让我们使用@@ RowCount。
UPDATE FormFillerAuto2_DomainCount SET [count] = ([count]+@count) where [domain] = @domain
If (@@ROWCOUNT > 0)
BEGIN
return 1 --updated : or return whatever you need to show it updated
END
ELSE
BEGIN
INSERT INTO FormFillerAuto2_DomainCount ([domain], [count]) VALUES (@domain, @count)
return 2 --inserted : or return whatever you need to show it inserted
END
答案 4 :(得分:0)
看起来你的更新有点搞砸..它应该是 更新TABLENAME SET COLUMNNAME = VALUE 条件
可读性有助于理解和维护代码。