将多个case语句返回的值存储在sql中

时间:2016-02-25 22:42:26

标签: sql sql-server database

我的多个case语句正在返回正确的值,但我需要将它们存储在某处,以便我可以在下一个查询中使用它们,我需要一个循环并在计算中使用这些值请建议

SELECT CASE WHEN v.JANORG  '0' THEN v.JANORG WHEN v.JANAREA  '0' THEN v.JANAREA ELSE p.JANORG END "JANORG",

 CASE WHEN v.FEBORG  '0' THEN v.FEBORG WHEN v.FEBAREA  '0' THEN v.FEBAREA ELSE p.FEBORG END  "FEBORG",

 CASE WHEN v.MARORG  '0' THEN v.MARORG WHEN v.MARAREA  '0' THEN v.MARAREA ELSE p.MARORG END  "MARORG",

 CASE WHEN v.APRORG  '0' THEN v.APRORG WHEN v.APRAREA  '0' THEN v.APRAREA ELSE p.APRORG END  "APRORG",

 CASE WHEN v.MAYORG  '0' THEN v.MAYORG WHEN v.MAYAREA  '0' THEN v.MAYAREA ELSE p.MAYORG END  "MAYORG",

 CASE WHEN v.JUNORG  '0' THEN v.JUNORG WHEN v.JUNAREA  '0' THEN v.JUNAREA ELSE p.JUNORG END  "JUNORG",

 CASE WHEN v.JULORG  '0' THEN v.JULORG WHEN v.JULAREA  '0' THEN v.JULAREA ELSE p.JULORG END  "JULORG",

 CASE WHEN v.AUGORG  '0' THEN v.AUGORG WHEN v.AUGAREA  '0' THEN v.AUGAREA ELSE p.AUGORG END  "AUGORG",

 CASE WHEN v.SEPORG  '0' THEN v.SEPORG WHEN v.SEPAREA  '0' THEN v.SEPAREA ELSE p.SEPORG END  "SEPORG",

 CASE WHEN v.OCTORG  '0' THEN v.OCTORG WHEN v.OCTAREA  '0' THEN v.OCTAREA ELSE p.OCTORG END  "OCTORG",

 CASE WHEN v.NOVORG  '0' THEN v.NOVORG WHEN v.NOVAREA  '0' THEN v.NOVAREA ELSE p.NOVORG END  "NOVORG",

 CASE WHEN v.DECORG  '0' THEN v.DECORG WHEN v.DECAREA  '0' THEN v.DECAREA ELSE p.DECORG END  "DECORG"    

FROM PRJFCAST v,SYSGEN_PROJ p
WHERE v.orgpt_id=3 and p.orgid=6416 and p.projid=18 and p.year=2017

1 个答案:

答案 0 :(得分:0)

我会将它们存储在变量中,以便稍后在过程中访问它们。

DECLARE @JANORG as Varchar(100)
DECLARE @FEBORG as Varchar(100)
DECLARE @MARORG as Varchar(100)
DECLARE @APRORG as Varchar(100)
DECLARE @MAYORG as Varchar(100)
DECLARE @JUNORG as Varchar(100)
DECLARE @JULORG as Varchar(100)
DECLARE @AUGORG as Varchar(100)
DECLARE @SEPORG as Varchar(100)
DECLARE @OCTORG as Varchar(100)
DECLARE @NOVORG as Varchar(100)
DECLARE @DECORG as Varchar(100)


SELECT 
@JANORG = CASE WHEN v.JANORG <> '0' THEN v.JANORG WHEN v.JANAREA <> '0' THEN v.JANAREA ELSE p.JANORG END,
@FEBORG = CASE WHEN v.FEBORG <> '0' THEN v.FEBORG WHEN v.FEBAREA <> '0' THEN v.FEBAREA ELSE p.FEBORG END,
@MARORG = CASE WHEN v.MARORG <> '0' THEN v.MARORG WHEN v.MARAREA <> '0' THEN v.MARAREA ELSE p.MARORG END,
@APRORG = CASE WHEN v.APRORG <> '0' THEN v.APRORG WHEN v.APRAREA <> '0' THEN v.APRAREA ELSE p.APRORG END,
@MAYORG = CASE WHEN v.MAYORG <> '0' THEN v.MAYORG WHEN v.MAYAREA <> '0' THEN v.MAYAREA ELSE p.MAYORG END,
@JUNORG = CASE WHEN v.JUNORG <> '0' THEN v.JUNORG WHEN v.JUNAREA <> '0' THEN v.JUNAREA ELSE p.JUNORG END,
@JULORG = CASE WHEN v.JULORG <> '0' THEN v.JULORG WHEN v.JULAREA <> '0' THEN v.JULAREA ELSE p.JULORG END,
@AUGORG = CASE WHEN v.AUGORG <> '0' THEN v.AUGORG WHEN v.AUGAREA <> '0' THEN v.AUGAREA ELSE p.AUGORG END,
@SEPORG = CASE WHEN v.SEPORG <> '0' THEN v.SEPORG WHEN v.SEPAREA <> '0' THEN v.SEPAREA ELSE p.SEPORG END,
@OCTORG = CASE WHEN v.OCTORG <> '0' THEN v.OCTORG WHEN v.OCTAREA <> '0' THEN v.OCTAREA ELSE p.OCTORG END,
@NOVORG = CASE WHEN v.NOVORG <> '0' THEN v.NOVORG WHEN v.NOVAREA <> '0' THEN v.NOVAREA ELSE p.NOVORG END,
@DECORG = CASE WHEN v.DECORG <> '0' THEN v.DECORG WHEN v.DECAREA <> '0' THEN v.DECAREA ELSE p.DECORG END
FROM PRJFCAST v,SYSGEN_PROJ p
WHERE v.orgpt_id=3 and p.orgid=6416 and p.projid=18 and p.year=2017