我想声明本地tsql变量,然后使用它们来连接子查询下的表

时间:2014-12-16 21:30:15

标签: ssms

DECLARE @strSQL nVARCHAR
DECLARE @CQ_ANLSID INT
DECLARE @LQ_ANLSID INT
DECLARE @CQEDM VARCHAR 
DECLARE @LQEDM VARCHAR 
DECLARE @CQRDM VARCHAR 
DECLARE @LQRDM VARCHAR 



SET @CQ_ANLSID = 220
SET @LQ_ANLSID = 4
SET @CQEDM = 'abc'
SET @CQRDM = 'efg'
SET @LQEDM = 'uvw'
SET @LQRDM = 'xyz'

SELECT @strSQL = 

'(
select DISTINCT R.anlsid AS CQ_ANLSID, T.anlsid AS LQ_ANLSID, R.AnalysisName AS CQ_ANLSNAME, T.AnalysisName AS LQ_ANLSNAME, R.ACCGRPID AS CQ_ACCGRPID, T.ACCGRPID AS LQ_ACCGRPID,
R.ACCGRPNAME AS CQ_ACCGRPNAME, T.ACCGRPNAME AS LQ_ACCGRPNAME,  
SUM(DISTINCT R.GU_Loss) AS CURR_Q_GU_LOSS, SUM(DISTINCT T.GU_Loss) AS LAST_Q_GU_LOSS, 
(ISNULL(SUM(R.GU_Loss)/SUM((CASE WHEN T.GU_Loss >0 THEN T.GU_Loss ELSE 1 END)),0)-1) AS GU_DIFF, 
SUM(DISTINCT R.GR_Loss) AS CURR_Q_GR_LOSS, SUM(DISTINCT T.GR_Loss) AS LAST_Q_GR_LOSS, 
(ISNULL(SUM(R.GR_Loss)/SUM((CASE WHEN T.GR_Loss>0 THEN T.GR_Loss ELSE 1 END)),0)-1) AS GR_DIFF, 
SUM(DISTINCT R.RL_Loss) AS CURR_Q_RL_LOSS, SUM(DISTINCT T.RL_Loss) AS LAST_Q_RL_LOSS ,
(ISNULL(SUM(R.RL_Loss)/SUM((CASE WHEN T.RL_Loss>0 THEN T.RL_Loss ELSE 1 END)),0)-1) AS RL_DIFF 

 FROM 
(   select ra.ID as anlsid,ra.NAME as AnalysisName,ag.ACCGRPID,ag.ACCGRPNAME,
    SUM(case when rl.PERSPCODE = GU then PUREPREMIUM else 0 end) AS GU_Loss,
    SUM(case when rl.PERSPCODE = GR then PUREPREMIUM else 0 end) AS GR_Loss,
    SUM(case when rl.PERSPCODE = RL then PUREPREMIUM else 0 end) AS RL_Loss
    FROM '+ @CQ_RDM +'..rdm_analysis ra 

    JOIN '+ @CQ_RDM +'..rdm_locstats rl on ra.ID = rl.ANLSID '+
'   JOIN '+ @CQ_EDM +'..loc l on rl.ID = l.LOCID '+
'   JOIN '+ @CQ_EDM +'..accgrp ag on l.ACCGRPID = ag.ACCGRPID '+
'   WHERE rl.ANLSID = '+ @CQ_ANLSID +'  
    GROUP BY ra.ID, ra.NAME, ag.ACCGRPID, ag.ACCGRPNAME ) R


FULL OUTER JOIN 
    ( select ra.ID as anlsid,ra.NAME as AnalysisName,ag.ACCGRPID,ag.ACCGRPNAME,
SUM(case when rl.PERSPCODE = '''GU''' then PUREPREMIUM else 0 end) AS GU_Loss,
SUM(case when rl.PERSPCODE = '''GR''' then PUREPREMIUM else 0 end) AS GR_Loss,
SUM(case when rl.PERSPCODE = '''RL''' then PUREPREMIUM else 0 end) AS RL_Loss
FROM '+ @LQ_RDM +'..rdm_analysis ra 

    JOIN '+ @LQ_RDM +'..rdm_locstats rl on ra.ID = rl.ANLSID '+ 
'   JOIN '+ @LQ_EDM +'..loc l on rl.ID = l.LOCID '+
'   JOIN '+ @LQ_EDM +'..accgrp ag on l.ACCGRPID = ag.ACCGRPID '+
'   WHERE rl.ANLSID  = '+ @LQ_ANLSID +'
GROUP BY ra.ID,ra.NAME,rl.id,ag.ACCGRPID,ag.ACCGRPNAME
) T ON T.ACCGRPNAME = R.ACCGRPNAME
WHERE T.ACCGRPNAME IS NOT NULL AND R.ACCGRPNAME IS NOT NULL

GROUP BY R.anlsid, R.AnalysisName, R.ACCGRPID, R.ACCGRPNAME, T.anlsid, T.ACCGRPID, T.ACCGRPNAME, T.AnalysisName )

UNION ALL 


select DISTINCT R.anlsid AS CQ_ANLSID, '' AS LQ_ANLSID, R.AnalysisName AS CQ_ANLSNAME, 
'' AS LQ_ANLSNAME, R.ACCGRPID AS CQ_ACCGRPID, '' AS LQ_ACCGRPID,
R.ACCGRPNAME AS CQ_ACCGRPNAME, ''New_Account'' AS LQ_ACCGRPNAME,  
SUM(R.GU_Loss) AS CURR_Q_GU_LOSS, '' AS LAST_Q_GU_LOSS, 
'' AS GU_DIFF, 
SUM(R.GR_Loss) AS CURR_Q_GR_LOSS, '' AS LAST_Q_GR_LOSS, 
'' AS GR_DIFF, 
SUM(R.RL_Loss) AS CURR_Q_RL_LOSS, '' AS LAST_Q_RL_LOSS ,
'' AS RL_DIFF 

 FROM 
(   select ra.ID as anlsid,ra.NAME as AnalysisName,ag.ACCGRPID,ag.ACCGRPNAME,
    SUM(case when rl.PERSPCODE = '''GU''' then PUREPREMIUM else 0 end) AS GU_Loss,
    SUM(case when rl.PERSPCODE = '''GR''' then PUREPREMIUM else 0 end) AS GR_Loss,
    SUM(case when rl.PERSPCODE = '''RL''' then PUREPREMIUM else 0 end) AS RL_Loss
    FROM '+ @CQ_RDM +'..rdm_analysis ra 

    JOIN '+ @CQ_RDM +'..rdm_locstats rl on ra.ID = rl.ANLSID '+
'   JOIN '+ @CQ_EDM +'..loc l on rl.ID = l.LOCID '+
'   JOIN '+ @CQ_EDM +'..accgrp ag on l.ACCGRPID = ag.ACCGRPID '+
'   WHERE rl.ANLSID = '+ @CQ_ANLSID +' 
    GROUP BY RA.ID, RA.NAME, ag.ACCGRPID, ag.ACCGRPNAME)R

FULL OUTER JOIN 
    ( select ra.ID as anlsid,ra.NAME as AnalysisName,ag.ACCGRPID,ag.ACCGRPNAME


FROM '+ @LQ_RDM +'..rdm_analysis ra 

    JOIN '+ @LQ_RDM +'..rdm_locstats rl on ra.ID = rl.ANLSID '+ 
'   JOIN '+ @LQ_EDM +'..loc l on rl.ID = l.LOCID '+
'   JOIN '+ @LQ_EDM +'..accgrp ag on l.ACCGRPID = ag.ACCGRPID '+
'   WHERE rl.ANLSID  = '+ @LQ_ANLSID +'
group by ra.ID,ra.NAME,ag.ACCGRPID,ag.ACCGRPNAME
) T ON T.ACCGRPNAME = R.ACCGRPNAME 
WHERE R.ACCGRPNAME IS NOT NULL AND T.ACCGRPNAME IS NULL

GROUP BY R.anlsid, R.AnalysisName, R.ACCGRPID, R.ACCGRPNAME


UNION ALL

select DISTINCT '' AS CQ_ANLSID, T.anlsid AS LQ_ANLSID, '' AS CQ_ANLSNAME, T.AnalysisName AS LQ_ANLSNAME, 
'' AS CQ_ACCGRPID, T.ACCGRPID AS LQ_ACCGRPID,
''Expired_Account'' AS CQ_ACCGRPNAME, T.ACCGRPNAME AS LQ_ACCGRPNAME,  
'' AS CURR_Q_GU_LOSS, SUM(DISTINCT T.GU_Loss) AS LAST_Q_GU_LOSS, 
'' AS GU_DIFF, 
'' AS CURR_Q_GR_LOSS, SUM(DISTINCT T.GR_Loss) AS LAST_Q_GR_LOSS, 
'' AS GR_DIFF, 
'' AS CURR_Q_RL_LOSS, SUM(DISTINCT T.RL_Loss) AS LAST_Q_RL_LOSS ,
'' AS RL_DIFF 


 FROM 
(   select ra.ID as anlsid,ra.NAME as AnalysisName,ag.ACCGRPID,ag.ACCGRPNAME
        FROM '+ @CQ_RDM +'..rdm_analysis ra 

    JOIN '+ @CQ_RDM +'..rdm_locstats rl on ra.ID = rl.ANLSID '+
'   JOIN '+ @CQ_EDM +'..loc l on rl.ID = l.LOCID '+
'   JOIN '+ @CQ_EDM +'..accgrp ag on l.ACCGRPID = ag.ACCGRPID '+
'   WHERE rl.ANLSID = '+ @CQ_ANLSID +'   
    group by ra.ID,ra.NAME,ag.ACCGRPID,ag.ACCGRPNAME)R


FULL OUTER JOIN 
    ( select ra.ID as anlsid,ra.NAME as AnalysisName,ag.ACCGRPID,ag.ACCGRPNAME,
SUM(case when rl.PERSPCODE = '''GU''' then PUREPREMIUM else 0 end) AS GU_Loss,
SUM(case when rl.PERSPCODE = '''GR''' then PUREPREMIUM else 0 end) AS GR_Loss,
SUM(case when rl.PERSPCODE = '''RL''' then PUREPREMIUM else 0 end) AS RL_Loss
FROM '+ @LQ_RDM +'..rdm_analysis ra 

    JOIN '+ @LQ_RDM +'..rdm_locstats rl on ra.ID = rl.ANLSID '+ 
'   JOIN '+ @LQ_EDM +'..loc l on rl.ID = l.LOCID '+
'   JOIN '+ @LQ_EDM +'..accgrp ag on l.ACCGRPID = ag.ACCGRPID '+
'   WHERE rl.ANLSID  = '+ @LQ_ANLSID +'
group by ra.ID,ra.NAME,ag.ACCGRPID,ag.ACCGRPNAME
) T ON T.ACCGRPNAME = R.ACCGRPNAME
WHERE R.ACCGRPNAME IS NULL AND T.ACCGRPNAME IS NOT NULL

GROUP BY T.anlsid, T.ACCGRPID, T.ACCGRPNAME, T.AnalysisName 
ORDER BY CQ_ACCGRPNAME




PRINT @strSQL

EXEC sp_executeSQL @strSQL
'

任何人都可以帮助学习如何在子查询中使用声明的值。我试图分析QoQ报告,除了accname之外,每件事情都在变化。有些名字是新的,有些是过期的,但大多数是更新的。

我在子查询的帮助下编写了查询并且它正在工作,但是在加入表时我没有多次使用数据库名称,而是想要声明并使用它们,但是,它们似乎不能在子查询下工作(不同的集合) )。

有人可以帮我吗?

谢谢你们。

1 个答案:

答案 0 :(得分:0)

您没有为字符串变量声明长度,因此默认情况下长度为30.

DECLARE @strSQL nVARCHAR

应该是

DECLARE @strSQL nVARCHAR(4000)

您的变量的声明方式与查询中使用的变量不同:

DECLARE @CQEDM VARCHAR 
DECLARE @LQEDM VARCHAR 
DECLARE @CQRDM VARCHAR 
DECLARE @LQRDM VARCHAR 

什么时候应该

DECLARE @CQ_EDM VARCHAR(25)
DECLARE @LQ_EDM VARCHAR(25)
DECLARE @CQ_RDM VARCHAR(25)
DECLARE @LQ_RDM VARCHAR(25)

,案例陈述应为

rl.PERSPCODE = ''' + 'GU' + '''

而不是

when rl.PERSPCODE = '''GU'''