0

我在这个代码片段中遇到了一个关于语法错误的问题

INSERT INTO tblScheduledBillGenerate(SettingConnectionId, UserId, MonthlyBill, BillGenerateDate)
VALUES ( @Id, @UserId, 
                        (   if exists (SELECT *
                            FROM tblServiceBillPackage_Audit WHERE UserId=4 and ServiceBillPackageId=(24) 
                            and Status=1 nd AuditType=N'UPDATE' ORDER BY tblServiceBillPackage_Audit.TimeStamp DESC)

BEGIN 

SELECT 1
                            
END
ELSE 
BEGIN
SELECT 2
END ), @daytoday)
Msg 156, Level 15, State 1, Procedure USP_SCHEDULER_TestBillSchedule, Line 50
Incorrect syntax near the keyword 'if'.
Msg 102, Level 15, State 1, Procedure USP_SCHEDULER_TestBillSchedule, Line 76
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Procedure USP_SCHEDULER_TestBillSchedule, Line 103
Incorrect syntax near 'END'.

上面给出了代码的问题部分。

4

1 回答 1

1

您可能想CASE在您的语句中使用 a 。IF可以在过程、触发器和函数中使用,但不能内联。

INSERT INTO tblScheduledBillGenerate(SettingConnectionId, UserId, MonthlyBill, BillGenerateDate)
SELECT @Id as SettingConnectionId,
       @UserId as UserId, 
       CASE WHEN (SELECT COUNT(Id)
                    FROM tblServiceBillPackage_Audit
                   WHERE UserId=4 and ServiceBillPackageId=(24) 
                     and Status=1 and AuditType=N'UPDATE') > 0
            THEN 1
            ELSE 2 END as MonthlyBill,
       @daytoday as BillGenerateDate;

这应该可以满足您的需求。

于 2021-05-06T04:13:27.550 回答