使用CASE WHEN语句在SQL中执行更新

时间:2015-12-03 11:33:37

标签: sql-server-2008

我想知道的是,在Case语句中是否可以以某种方式对表进行更新?或者,如果有某种方法可以做到这一点?我有一个拉动报告的按钮(此按钮会调用存储过程,并获取列及其驻留列的列表)。

我基本上想要更新表,IF,并且只有IF,DateClosed列为NULL且ClosedBy为空(即达到结束日期,因此广告系列已关闭)。

我通常决定广告系列是否已关闭的方式是在开始日期和结束日期进行检查。即EndDate< GetDate()然后“关闭”。显然,这显示正确,但我的数据库显示为null,因为没有更新。

这是我的存储过程:

ALTER PROCEDURE [dbo].[sp_CampaignStats]
    @from DATETIME,
    @to DATETIME
AS
    SELECT 
        CampaignName AS 'CAMPAIGN NAME',
        CampaignDescription AS 'CAMPAIGN DESCRIPTION',
        CASE
           WHEN EndDate >= GETDATE() AND StartDate <= GETDATE() THEN 'Active' 
           WHEN StartDate >= GETDATE() THEN 'Pending'
           WHEN CampaignStatus = 4 THEN 'Archived'
           ELSE 'Closed'
        END as 'CurrentStatus',
        CONVERT(VARCHAR(11), StartDate, 106) + ' - ' + CONVERT(VARCHAR(11), EndDate, 106) AS 'CAMPAIGN DATES',
        Discount AS 'DISCOUNT',
        [Target] AS 'TARGET',
        Uptake AS 'UPTAKE',
        tc.DateAdded as 'DATE ADDED',
        U.FirstName + ' ' + u.LastName As 'ADDED BY',
        CASE 
           WHEN CloseBy IS NULL AND EndDate < GETDATE() AND CloseBy = Null THEN 'System'
           WHEN CampaignStatus = 4 THEN 'Archived'
           WHEN CloseBy IS not Null THEN UU.FirstName + ' ' +  uu.LastName
           ELSE 'Not Closed'
        END AS 'CLOSED BY',
        DateClosed AS 'DATE CLOSED'
    FROM 
        Tbl_Campaign tc
    LEFT JOIN 
        tbl_User U ON Tc.AddedBy = U.UserId 
    LEFT JOIN 
        Tbl_User UU ON TC.CloseBy = UU.UserId
    WHERE 
       (startDate >= @from OR enddate <= @to)
       AND CampaignStatus IN (1, 2, 3, 4)

2 个答案:

答案 0 :(得分:1)

所以你想在发生SELECT之前更新你的表吗?

尝试这样的事情:

UPDATE dbo.Tbl_Campaign
SET  -- what do you want to update to what value here????
WHERE 
    DateClosed IS NULL
    AND ClosedBy IS NULL

或者你到底想要做什么?你的问题中你仍然不清楚和含糊不清 - 你没有在 WHICH 表中提及要更新为 WHAT WHAT 列如果满足该条件,则为值.....

答案 1 :(得分:0)

SQL只允许每个语句执行一次操作。 SELECT只读数据,UPDATE只写数据。

如果我已经理解了你的问题,你在结束日期字段中有一个NULL,这会阻止你正确显示你的按钮?如果是这种情况,您可以执行类似

的操作
SELECT
    StatusDate = 
        CASE WHEN EndDate < GETDATE() THEN 'Closed'
            WHEN EndDate > GETDATE() THEN 'Open'
            WHEN EndDate IS NULL THEN 'End Date not set'
        END
FROM
    SourceTable

或者,如果代码更容易,请将CASE包装在ISNULL或COALESCE函数中,该函数可用于填充默认值。

这些都不会更新读取表,但它们都会阻止NULL阻止您的读取返回任何内容。

现在看到你的实际代码,你想要的是这样的 -

ALTER PROCEDURE [dbo].[usp_CampaignStats]
    @from DATETIME,
    @to DATETIME
AS
    SET NOCOUNT ON;

    --Make sure any NULLs we need to have been cleaned up
    UPDATE Tbl_Campaign
    SET    <Fields>
    WHERE  DateClosed IS NULL AND ClosedBy IS NULL
           AND (startDate >= @from OR enddate <= @to)
           AND CampaignStatus IN (1, 2, 3, 4);

    --Now return the data
    SELECT 
        CampaignName AS 'CAMPAIGN NAME',
        CampaignDescription AS 'CAMPAIGN DESCRIPTION',
        CASE
           WHEN EndDate >= GETDATE() AND StartDate <= GETDATE() THEN 'Active' 
           WHEN StartDate >= GETDATE() THEN 'Pending'
           WHEN CampaignStatus = 4 THEN 'Archived'
           ELSE 'Closed'
        END as 'CurrentStatus',
        CONVERT(VARCHAR(11), StartDate, 106) + ' - ' + CONVERT(VARCHAR(11), EndDate, 106) AS 'CAMPAIGN DATES',
        Discount AS 'DISCOUNT',
        [Target] AS 'TARGET',
        Uptake AS 'UPTAKE',
        tc.DateAdded as 'DATE ADDED',
        U.FirstName + ' ' + u.LastName As 'ADDED BY',
        CASE 
           WHEN CloseBy IS NULL AND EndDate < GETDATE() AND CloseBy = Null THEN 'System'
           WHEN CampaignStatus = 4 THEN 'Archived'
           WHEN CloseBy IS not Null THEN UU.FirstName + ' ' +  uu.LastName
           ELSE 'Not Closed'
        END AS 'CLOSED BY',
        DateClosed AS 'DATE CLOSED'
    FROM 
        Tbl_Campaign tc
    LEFT JOIN 
        tbl_User U ON Tc.AddedBy = U.UserId 
    LEFT JOIN 
        Tbl_User UU ON TC.CloseBy = UU.UserId
    WHERE 
       (startDate >= @from OR enddate <= @to)
       AND CampaignStatus IN (1, 2, 3, 4);