在case语句中返回列名但不涉及表

时间:2014-02-20 23:29:11

标签: sql sql-server sql-server-2008

您好任何人都可以帮助解决下面的问题,我有一个存储过程,具体取决于月份和日期,取决于返回的图像。我已经找到了很多关于如何在表格中解决这个问题的例子,但没有关于我如何解决这个问题的解决方案。

我这样做的原因是因为在程序中修改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执行ok,但是当我想要的是url作为列名时,列是(No Column Name)。

任何有经验的人的帮助都会受到赞赏。

我正在使用SQL2008R2

5 个答案:

答案 0 :(得分:2)

您不需要内部SELECT

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
顺便说一句,3月19日将永远不会是复活节的星期天。 3月22日是最早的Easter date

答案 1 :(得分:1)

AS URL需要位于案例陈述的末尾

CASE
...
END AS URL

答案 2 :(得分:0)

在case语句中添加列别名。案例陈述是if / then命题,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 as MyURL --edit to your liking

  -- return @URL
END 

答案 3 :(得分:0)

在案例陈述后添加您的列名:

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

答案 4 :(得分: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