2014-02-20 48 views
0

您好任何人都可以幫助解決以下問題,我有一個取決於月份和日期的存儲過程取決於返回哪個圖像。當數據在表格中時,我發現了很多關於如何解決這個問題的例子,但是我沒有解決這個問題。在case case語句中返回列名,但沒有涉及表

我之所以這樣做是因爲在程序中修改sql比改變和上傳新代碼到網站更容易。

DECLARE @CurrentMonth int 
SET @CurrentMonth = (MONTH(GETDATE())) 

DECLARE @CurrentDate int 
SET @CurrentDate = (Day(GETDATE())) 

--DECLARE @Url varchar(100) 
--SET @URL = '' 


    SET NOCOUNT ON; 

    SELECT 
      --Set image for xmas 
      CASE WHEN @CurrentMonth = 12 AND @CurrentDate = 25 
      THEN (SELECT 'imagetest.png' AS URL) 
      --Set for easter 
      WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
      THEN (SELECT 'imagetest2.png' AS url) 
      --Keep setting images for events 
      WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
      THEN (SELECT 'imagetest3.png' AS url) 
      --If no match, return default image 
      ELSE (SELECT 'logo.png' AS url) 
     END 

     -- return @URL 
    END 

sp執行正常,但當我想要url是列名時,列是(無列名)。

任何有經驗的人的幫助將不勝感激。

我使用SQL2008R2

回答

2

你不需要內SELECT S:

SELECT 
     --Set image for xmas 
     CASE WHEN @CurrentMonth = 12 AND @CurrentDate = 25 
     THEN 'imagetest.png' 
     --Set for easter 
     WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
     THEN 'imagetest2.png' 
     --Keep setting images for events 
     WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
     THEN 'imagetest3.png' 
     --If no match, return default image 
     ELSE 'logo.png' 
    END AS url 

BTW,3月19日絕不會是一個復活節。 3月22日是最早可能的Easter date

+0

嗨D斯坦利我知道,我只是把任何日期,以檢查它是否有效:) –

+0

D斯坦利我需要等待3分鐘才能授予答案,有趣的是,當你看到答案時看起來如此簡單,當時我是ra my着我的大腦,試圖弄明白這一點 –

0

向case語句添加列別名。 Case語句是if/then命題,SQL Server不會根據測試的列分配名稱。你可以自由地命名它們。

DECLARE @CurrentMonth int 
SET @CurrentMonth = (MONTH(GETDATE())) 

DECLARE @CurrentDate int 
SET @CurrentDate = (Day(GETDATE())) 

--DECLARE @Url varchar(100) 
--SET @URL = '' 


SET NOCOUNT ON; 

SELECT 
     --Set image for xmas 
     CASE WHEN @CurrentMonth = 12 AND @CurrentDate = 25 
     THEN (SELECT 'imagetest.png' AS URL) 
     --Set for easter 
     WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
     THEN (SELECT 'imagetest2.png' AS url) 
     --Keep setting images for events 
     WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
     THEN (SELECT 'imagetest3.png' AS url) 
     --If no match, return default image 
     ELSE (SELECT 'logo.png' AS url) 
    END as MyURL --edit to your liking 

    -- return @URL 
END 
1

AS URL需要在你的case語句

CASE 
... 
END AS URL 
0

結束你的case語句後添加您的列名:

SELECT 
    --Set image for xmas 
     CASE WHEN @CurrentMonth = 12 AND @CurrentDate = 25 
     THEN (SELECT 'imagetest.png' AS URL) 
     --Set for easter 
     WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
     THEN (SELECT 'imagetest2.png' AS url) 
     --Keep setting images for events 
     WHEN @CurrentMonth = 3 AND @CurrentDate = 19 
     THEN (SELECT 'imagetest3.png' AS url) 
     --If no match, return default image 
     ELSE (SELECT 'logo.png' AS url) 
    END AS url 
0

這是一個不同的方式,你可以這樣做可能更容易維護:

select top 1 Url from (
    select Mon = null, Dat = null, Url = 'logo.png' union all 
    select 12, 25, 'imagetest.png' union all 
    select 3, 19, 'imagetest2.png' union all 
    select 3, 19, 'imagetest3.png' 
) x 
where (Mon = @CurrentMonth and Dat = @CurrentDate) or (Mon is null) 
order by Mon desc