繁体   English   中英

SQL Server存储过程编译错误

[英]SQL Server stored procedure compile error

存储过程是:

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”附近的语法不正确。

你的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

EXISTS

指定要测试行是否存在的子查询。 如果子查询包含任何行,则返回TRUE ,它接受restricted SELECT statement ,不允许使用INTO关键字。

这里的问题是你不能在Exists中设置值。

尝试

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

我相信你不见了; 在这里标记声明的结尾。 同样, 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

你使用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

看起来你的更新有点搞砸..它应该是UPDATE TABLENAME SET COLUMNNAME = VALUE WHERE CONDITION

可读性有助于理解和维护代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM