0

我正在创建一个存储过程,它查询表“条目”。proc 有参数@ID,它是一个int,和@APPROVED,它是一个位。我正在使用 SQL Server 2005

如果批准是假的,我想做一些与它是真的不同的事情。我写了以下内容。当我尝试创建它时,我得到“关键字'END'附近的语法不正确。”。

如果我删除嵌套如果错误消失但是从我读过的内容来看这是完全有效的语法。谁能告诉我哪里出错了?

CREATE Procedure [dbo].[GetEntry](@ID int,@APPROVED bit)
AS
IF @APPROVED = 0
BEGIN 
    --see if the unapproved entry has already been viewed 
    IF (SELECT COUNT(*) 
    FROM [dbo].[Entries]
    WHERE EntryId = @ID AND Approved = @APPROVED AND Viewed = 0) > 0
    BEGIN

    END

END

任何帮助将非常感激。谢谢!

4

2 回答 2

1

您需要在内部做一些事情,与手动获取计数(必须扫描整个聚集索引)相比,BEGIN/END更有效的检查是。EXISTS你真的不在乎结果是 2 还是 5 还是 27,654,对吧?

IF EXISTS (SELECT 1 FROM dbo.Entries WHERE ...)
BEGIN
    PRINT 'It exists!';
END
于 2012-09-19T17:43:29.863 回答
1

您必须在 中做一些事情BEGIN/END,它不能为空:

CREATE Procedure [dbo].[GetEntry](@ID int,@APPROVED bit)
AS
IF @APPROVED = 0
BEGIN 
    --see if the unapproved entry has already been viewed 
    IF (SELECT COUNT(*) 
            FROM [dbo].[Entries]
            WHERE EntryId = @ID AND Approved = @APPROVED AND Viewed = 0) > 0
    BEGIN
        -- do something here
        select *
        from [dbo].[Entries]
    END

END
于 2012-09-19T17:43:46.033 回答