0

我正在我的 SQL Server 存储过程中编写 CASE 语句。对于 11 个 CASE,我每次都重复相同的长 SQL 语句。我应该将 SQL 语句放在另一个存储过程中的 CASE 条件下吗?什么可能是最好的方法?

CASE (SELECT ..................)
THEN  'SELCET a.field'
ELSE
 ''SELECT vlaues'
END
as 'Coulmn1 ' 

CASE (SELECT ..................)
THEN  'SELCET vaues'
ELSE
 ''SELECT vlaues'
END
as 'Coulmn1 ' 
4

4 回答 4

1

你可能想做类似的事情

SELECT
CASE (your select statement which retuns one value)
WHEN 'option1' THEN 'value2return1'
WHEN 'option2' THEN 'value2return2'
WHEN 'option3' THEN 'value2return3'
...
ELSE 'defaultValue'
END
于 2013-07-28T16:17:05.370 回答
0

因此尝试使用重复查询一次:

select A.C1, X.C2, X.C3
  from TableA as A inner join
    ( select B.AlmondJoy,
        case when B.YN > 7 then 'Yes' else 'No' end as C2,
        case when C.UD in ( -1, 1 ) then 'Up' else 'Down' end as C3
        from TableB as B inner join
          TableC as C on C.Id = B.Id
      ) as X on X.AlmondJoy = A.Zagnut
于 2013-07-28T18:05:23.753 回答
0

使用公用表表达式链接

    WITH CTE (ColA)
    AS
    (
        Select ColA From Table
    )
    SELECT ColA
    FROM CTE
于 2013-07-28T16:18:47.663 回答
0

使用 CTE。将来,如果您给我们一个更好的代码示例,这将有所帮助。

WITH RepeateSelect AS (SELECT ..................)
.....
.....
CASE (SELECT * FROM RepeateSelect)
THEN  'SELCET a.field'
ELSE
 'SELECT vlaues'
END
as 'Coulmn1 ' 

CASE (SELECT * FROM RepeateSelect)
THEN  'SELCET vaues'
ELSE
 ''SELECT vlaues'
END
as 'Coulmn1 ' 
于 2013-07-28T16:19:08.183 回答