SQL Server IF NOT EXISTS 用法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1175217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:57:00  来源:igfitidea点击:

SQL Server IF NOT EXISTS Usage?

sqlsql-serverstored-procedures

提问by Ryan Kirkman

Ok, so my schema is this:

好的,所以我的架构是这样的:

Table: Timesheet_Hours

表:时间表_小时

Columns:

列:

  • Timesheet_Id (PK, int)
  • Staff_Id (int)
  • BookedHours (int)
  • Posted_Flag (boolean)
  • Timesheet_Id (PK, int)
  • Staff_Id (int)
  • 预订时间(整数)
  • Posted_Flag(布尔值)

This is an extremely simplified version of the table, but it will serve for the purposes of this explaination. Assume that a person can only ever have one timesheet record.

这是该表的极其简化的版本,但将用于本说明的目的。假设一个人只能有一个时间表记录。

What I'm trying to do is add records to another table, called WorkLog. Each record here has a time associated with it. When that table is updated, I want to update Timesheet_Hours as well.

我想要做的是将记录添加到另一个名为 WorkLog 的表中。这里的每条记录都有一个与之相关的时间。当该表更新时,我也想更新 Timesheet_Hours。

Before I update Timesheet_Hours, I want to check first that the relevant Timesheets haven't already been posted, and then I want to check if there is in fact a record to update in the first place.

在我更新 Timesheet_Hours 之前,我想首先检查相关的 Timesheets 是否尚未发布,然后我想首先检查是否确实有要更新的记录。

The first part of the if statement, which checks to see that the timesheets haven't already been posted, works fine. The problem is the second part. It is checkint to see that the record it is going to update already exists. The issue is that it always raises an error.

if 语句的第一部分用于检查时间表是否尚未发布,工作正常。问题是第二部分。checkint 查看它要更新的记录是否已经存在。问题是它总是会引发错误。

NB: The code below is extracted from a stored procedure run by the update, insert and delete triggers on the WorkLog table. @PersonID is one of the parameters to that table. The stored procedure works fine if I comment out the second part of this statement.

注意:下面的代码是从 WorkLog 表上的更新、插入和删除触发器运行的存储过程中提取的。@PersonID 是该表的参数之一。如果我注释掉该语句的第二部分,存储过程就可以正常工作。

IF EXISTS
    (
    SELECT 1
    FROM Timesheet_Hours
    WHERE Posted_Flag = 1
    AND Staff_Id = @PersonID
    )

    BEGIN
        RAISERROR('Timesheets have already been posted!', 16, 1)
        ROLLBACK TRAN
    END
ELSE
    IF NOT EXISTS
        (
        SELECT 1
        FROM Timesheet_Hours
        WHERE Staff_Id = @PersonID
        )

        BEGIN
            RAISERROR('Default list has not been loaded!', 16, 1)
            ROLLBACK TRAN
        END

回答by Rick

Have you verified that there is in fact a row where Staff_Id = @PersonID? What you've posted works fine in a test script, assuming the row exists. If you comment out the insert statement, then the error is raised.

您是否验证过实际上有一行 Staff_Id = @PersonID?假设该行存在,您发布的内容在测试脚本中运行良好。如果注释掉插入语句,则会引发错误。

set nocount on

create table Timesheet_Hours (Staff_Id int, BookedHours int, Posted_Flag bit)

insert into Timesheet_Hours (Staff_Id, BookedHours, Posted_Flag) values (1, 5.5, 0)

declare @PersonID int
set @PersonID = 1

IF EXISTS    
    (
    SELECT 1    
    FROM Timesheet_Hours    
    WHERE Posted_Flag = 1    
        AND Staff_Id = @PersonID    
    )    
    BEGIN
        RAISERROR('Timesheets have already been posted!', 16, 1)
        ROLLBACK TRAN
    END
ELSE
    IF NOT EXISTS
        (
        SELECT 1
        FROM Timesheet_Hours
        WHERE Staff_Id = @PersonID
        )
        BEGIN
            RAISERROR('Default list has not been loaded!', 16, 1)
            ROLLBACK TRAN
        END
    ELSE
        print 'No problems here'

drop table Timesheet_Hours