SQL Server IF NOT EXISTS 用法? [英] SQL Server IF NOT EXISTS Usage?

查看:86
本文介绍了SQL Server IF NOT EXISTS 用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, so my schema is this:

表:Timesheet_Hours

Table: Timesheet_Hours

列:

  • 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.

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

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.

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

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.

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

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 checking to see that the record it is going to update already exists. The issue is that it always raises an error.

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

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.

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

推荐答案

您是否确认确实存在 Staff_Id = @PersonID 的行?假设该行存在,您发布的内容在测试脚本中运行良好.如果您注释掉插入语句,则会引发错误.

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.

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

这篇关于SQL Server IF NOT EXISTS 用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆