2014-02-20 127 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