简体   繁体   English

SQL Server IF NOT EXISTS 用法?

[英]SQL Server IF NOT EXISTS Usage?

Ok, so my schema is this:好的,所以我的架构是这样的:

Table: Timesheet_Hours表:Timesheet_Hours

Columns:列:

  • Timesheet_Id (PK, int) Timesheet_Id (PK, int)
  • Staff_Id (int) Staff_Id (int)
  • BookedHours (int)预订时间(整数)
  • Posted_Flag (boolean) 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.我想要做的是将记录添加到另一个名为 WorkLog 的表中。 Each record here has a time associated with it.这里的每条记录都有一个与之相关的时间。 When that table is updated, I want to update Timesheet_Hours as well.当该表更新时,我也想更新 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. if 语句的第一部分用于检查时间表是否尚未发布,工作正常。 The problem is the second part.问题是第二部分。 It is checking to see that the record it is going to update already exists.它正在检查要更新的记录是否已经存在。 The issue is that it always raises an error.问题是它总是会引发错误。

NB: The code below is extracted from a stored procedure run by the update, insert and delete triggers on the WorkLog table.注意:下面的代码是从 WorkLog 表上的更新、插入和删除触发器运行的存储过程中提取的。 @PersonID is one of the parameters to that table. @PersonID 是该表的参数之一。 The stored procedure works fine if I comment out the second part of this statement.如果我注释掉该语句的第二部分,存储过程就可以正常工作。

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

Have you verified that there is in fact a row where Staff_Id = @PersonID?您是否验证过实际上有一行 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.如果注释掉插入语句,则会引发错误。

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

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

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