将虚拟行插入CTE动态构造(在动态CTE中使用IF EXISTS?)

时间:2016-02-11 15:30:28

标签: tsql if-statement common-table-expression dynamic-sql

我的下方构建取决于收款。

根据各种情况需要调整最终报告(例如,在某个日期确定第一行为零,第二行显示从所​​需现金信用中扣除的信用付款)。

我需要根据ERP中指定的各种条件构建适合能够创建 IMAGINARY ROWS 的CTE ...

 INSERT  INTO @PLATBY
                    ( ROWNUMBER ,
                      OD ,
                      DO ,
                      UHRADA
                    )
                    EXECUTE
                           ( 'WITH PLATBY AS (
                    SELECT
                    rownum = ROW_NUMBER() OVER (ORDER BY FAK.DATUM),
                    FAK.DATUM,SUM(FAK.CASTKA) AS CASTKA
                    FROM (
                    SELECT FAPLATV.FPV_DATUM AS DATUM,SUM(FAPLATV.FPV_CASTKA) AS CASTKA
                    FROM ' + @DATABASE + '..CG_OP7UC UC
                    INNER JOIN ' + @DATABASE
                             + '..CG_OP7FV FVxCO ON FVxCO.CODE = UC.CODE
                    INNER JOIN ' + @DATABASE
                             + '..CG_POZN POZNAMKA ON POZNAMKA.ID=FVxCO.TYP AND POZNAMKA.ZKRATKA in (''UB'',''UDB'',''UP'',''UDP'')
                    INNER JOIN ' + @DATABASE
                             + '..FAKTURYV FAKTURYV ON FAKTURYV.CISLO_FV = FVxCO.CISLO_FV
                    INNER JOIN ' + @DATABASE
                             + '..FAPLATV FAPLATV ON FAPLATV.FPV_CISLO = FAKTURYV.CISLO_FV
                    WHERE UC.CODE LIKE ''' + @CODE + '''
                    GROUP BY FAPLATV.FPV_DATUM
                    UNION ALL
                    --Pridani Faktur s DPH
                    SELECT FVxCO.DAT AS DATUM,-FAKTURYV.CASTKA AS CASTKA
                    FROM ' + @DATABASE + '..CG_OP7UC UC
                    INNER JOIN ' + @DATABASE
                             + '..CG_OP7FV FVxCO ON FVxCO.CODE = UC.CODE
                    INNER JOIN ' + @DATABASE
                             + '..CG_POZN POZNAMKA ON POZNAMKA.ID=FVxCO.TYP AND POZNAMKA.ZKRATKA in (''UDB'',''UDP'')
                    INNER JOIN ' + @DATABASE
                             + '..FAKTURYV FAKTURYV ON FAKTURYV.CISLO_FV = FVxCO.CISLO_FV
                    WHERE UC.CODE LIKE ''' + @CODE + '''
                    SELECT UC.DATSAZ  AS DATUM, UC.UVER AS CASTKA
                    FROM ' + @DATABASE + '..CG_OP7UC UC
                    WHERE  UC.CODE LIKE ''' + @CODE
                             + '''
                     )FAK
                    GROUP BY FAK.DATUM
                    )
                    SELECT PLATBY.rownum,prev.DATUM AS OD,PLATBY.DATUM as DO,PLATBY.CASTKA AS UHRADA
                    FROM PLATBY
                    LEFT JOIN PLATBY prev ON prev.rownum = PLATBY.rownum - 1
                    LEFT JOIN PLATBY nex ON nex.rownum = PLATBY.rownum + 1
                    '
                           );

1 个答案:

答案 0 :(得分:0)

Resolution: 1. check statement, 2. construct query as string 

SET @sqlstatement = ( 'SELECT @result=CASE 
WHEN UC._DATDOKON IS NULL AND ISNULL(UC.UVER, 0) > 0  THEN 1 
WHEN UC._DATDOKON IS NULL AND ISNULL(UC.UVER, 0) = 0 THEN 2 
WHEN UC._DATDOKON IS NOT NULL AND ISNULL(UC.UVER, 0) > 0 THEN 3
WHEN UC._DATDOKON IS NOT NULL AND ISNULL(UC.UVER, 0) = 0 THEN 4
ELSE 0 END 
FROM    ' + @DATABASE + '..CG_OP7UC UC
INNER JOIN ' + @DATABASE
+ '..FAKTURYV PL ON PL.CODE = UC.CODE
WHERE   UC.CODE LIKE ''' + @CODE + '''' );

SET @parmDefinition = N'@result INT OUTPUT';
EXEC sp_executesql @sqlstatement, @parmDefinition,
@result = @EXISTS OUTPUT;

2. construct Query as string:
SET @FQUERY = ISNULL(@QUERYHEADER, '') + ISNULL(@QUERYVER1, '')
+ ISNULL(@QUERYVER2, '') + ISNULL(@QUERYVER3, '')
+ ISNULL(@QUERYVER4, '') + ISNULL(@QUERYFOOTER, '');
PRINT ( @FQUERY );

INSERT  INTO @PLATBY
 ( ROWNUMBER ,
OD ,
DO ,
UHRADA
)
EXECUTE ( @FQUERY);