TSQL案例声明更新表

时间:2014-05-28 20:39:03

标签: tsql

我正在尝试设置我认为需要成为我的存储过程的案例陈述

根据@status的值,我只需要更新表中的特定列。

例如:如果@status ='已批准',那么我需要更新approvalDate字段;同样@status被'拒绝'我需要将当前日期输入拒绝字段。

我该如何处理?

 UPDATE  tuitionSubmissions
        SET reimbursementDate = NullIf (@reimbursementDate, '1 jan 1900'),
            empGradDate       = NullIf (@gradDate, '1 jan 1900'),
            payPeriod         = NullIF (@payPeriod, '1 jan 1900'),
            [status]          = @status,
            notes             = @notes,
            managerApproval   = @empID,
            denialDate        = GETDATE(),   
            approvalDate      = GETDATE()
    WHERE   id = @tid;

1 个答案:

答案 0 :(得分:1)

 UPDATE  tuitionSubmissions
        SET reimbursementDate = NullIf (@reimbursementDate, '1 jan 1900'),
            empGradDate       = NullIf (@gradDate, '1 jan 1900'),
            payPeriod         = NullIF (@payPeriod, '1 jan 1900'),
            [status]          = @status,
            notes             = @notes,
            managerApproval   = @empID,
            denialDate        = case when @status = 'Denied'then GETDATE() else  denialDate end,   
            approvalDate      = case when @status = 'Approved'then GETDATE() else  denialDate end
    WHERE   id = @tid;