如果不存在不能正常工作

时间:2017-09-06 09:32:00

标签: sql sql-server stored-procedures

我遇到SQL Server存储过程的问题。我试图检查两个表中是否已存在具有相同ID的数据。

ALTER PROCEDURE [dbo].[Objective_Comment_Insert]
    @CompanyName AS NVARCHAR(100)
AS
BEGIN
    IF NOT EXISTS (SELECT * 
                   FROM dbo.[SB$Objective Comment] oc, [dbo].[comments] cc
                   WHERE CONVERT(INT, oc.[Employee No_]) = (SELECT u1.employee_no 
                                                            FROM c0_intranet.dbo.users u1 
                                                            WHERE u1.user_id = cc.user_id) 
                     AND oc.[No_] = cc.comment_id)
    BEGIN
        INSERT INTO dbo.[SB$Objective Comment]([Comment By], [Comment], [Date Created],
                                               [No_], [Comment On], [Employee No_])
            SELECT  
                (SELECT u1.employee_no 
                 FROM c0_intranet.dbo.users u1 
                 WHERE u1.user_id = cs.user_id
                ),
                CASE 
                   WHEN cs.comment IS NULL 
                      THEN '' 
                      ELSE cs.comment 
                END,
                CASE 
                   WHEN cs.date_created IS NULL 
                      THEN FORMAT(GETDATE(), 'yyyy-MM-dd') 
                      ELSE FORMAT(cs.date_created, 'yyyy-MM-dd') 
                END,
                cs.comment_id,
                CASE 
                   WHEN cs.comment_on IS NULL 
                      THEN 0 
                      ELSE cs.comment_on 
                END,
                (SELECT u1.employee_no 
                 FROM dbo.users u1 
                 WHERE u1.user_id = cs.user_id)
            FROM 
                dbo.comments cs
    END
    ELSE
    BEGIN
        UPDATE NAV_2017.dbo.[SB$Objective Comment]
        SET [Comment By] = (SELECT u1.employee_no 
                            FROM dbo.users u1 
                            WHERE u1.user_id = cs.user_id),
            [Comment] = CASE 
                           WHEN cs.comment IS NULL 
                              THEN '' 
                              ELSE cs.comment 
                        END,
            [Date Created] = CASE 
                                WHEN cs.date_created IS NULL 
                                   THEN FORMAT(GETDATE(), 'yyyy-MM-dd')  
                                   ELSE FORMAT(cs.date_created, 'yyyy-MM-dd') 
                             END,
            [No_] = cs.comment_id,
            [Comment On] = CASE 
                              WHEN cs.comment_on IS NULL 
                                 THEN 0 
                                 ELSE cs.comment_on 
                           END,
            [Employee No_] = (SELECT u1.employee_no 
                              FROM dbo.users u1 
                              WHERE u1.user_id = cs.user_id)
        FROM dbo.comments cs, NAV_2017.dbo.[SB$Objective Comment] oc
        WHERE 
            CONVERT(INT, oc.[Employee No_]) = (SELECT u1.employee_no 
                                               FROM dbo.users u1 
                                               WHERE u1.user_id = cs.user_id)
            AND oc.[No_] = cs.comment_id 
    END
END

当它第一次执行时,第二个表中没有数据,并且在我在第一个表中添加另一个记录之后它没有工作,它没有将它插入第二个表但是没有错误。

This is when I execute the procedure inside exists

This is data from table comments

2 个答案:

答案 0 :(得分:0)

如果我正确地重建了你的逻辑,那么这个查询就等于你的了:

select 1
from [dbo].[comments] cc
    join c0_intranet.dbo.users u1 on u1.user_id = cc.user_id
    join dbo.[SB$Objective Comment] oc on oc.[No_] = cc.comment_id and CONVERT(int, oc.[Employee No_] = u1.employee_no

如果有,如果有任何数据已经复制到dbo.[SB$Objective Comment],它将始终返回行,请尝试替换为:

select 1
from [dbo].[comments] cc
    join c0_intranet.dbo.users u1 on u1.user_id = cc.user_id
    left outer join dbo.[SB$Objective Comment] oc on oc.[No_] = cc.comment_id and CONVERT(int, oc.[Employee No_] = u1.employee_no
where oc.[No_] is null

答案 1 :(得分:0)

我认为你的方法在这里是错误的,你应该看一个触发器而不是一个程序。具体来说,我认为你已经在注释中插入了6行,执行了该过程,然后将另一行插入注释,然后再执行该过程。问题是,在第一次调用时,存在的测试失败(即它不存在),因此插入了注释。在第二次调用时,存在的测试通过(确实存在6条记录),因此插入未完成。该过程无法知道注释中插入了什么,这就是为什么触发器(它确实知道插入了什么)将是我的选择以及用于插入注释的合适的存在测试。