zoukankan      html  css  js  c++  java
  • serversql -----行转列,动态sql 实例

    USE TRAN_C_C
    GO
    /*===================================================================================================
    名称: L611_C_STK_PVSAIPM_SUB_2_C_STK_ASSET_DEPR
    功能: 实时数据转换:
    源表: CGENIUS..上市公司资产减值准备明细主表 STK_PVSAIPM_MAIN
    CGENIUS..上市公司资产减值准备明细子表 STK_PVSAIPM_SUB
    目的表:CGENIUS..资产减值准备明细表 STK_ASSET_DEPR
    作者: GINA
    文档编号:$/数据开发部/B.巨灵新数据库设计/01-股票/14-财务附注/04-转
    换文档(CG到CG)/L611-STK_PVSAIPM_MAIN(上市公司资产减值准备明细主表)_2_STK_ASSET_DEPR(资产减值准备明细表)CG2CG转换文档.XLSX

    说 明:
    参数: SEQ
    返回结果:结果集
    创建日期:2020-8-26

    select 'STK_ASSET_DEPR','C_STK_PVSAIPM_SUB_2_C_STK_ASSET_DEPR',seq,''
    from cgenius..STK_PVSAIPM_SUB
    ===================================================================================================*/
    CREATE PROCEDURE C_STK_PVSAIPM_SUB_2_C_STK_ASSET_DEPR (@SEQ INT) AS
    BEGIN

    --DECLARE @SEQ INT =4
    DECLARE
    @Y_P_SEQ  INT
    ,@Y_ITEM VARCHAR (200)
    ,@Y_ISVALID INT
    ,@Y_ITEM_ID INT
    ,@FILE VARCHAR (30)

    SELECT
    @Y_P_SEQ  =P_SEQ 
    ,@Y_ITEM =ITEM
    ,@Y_ITEM_ID =ITEM_ID
    ,@Y_ISVALID =ISVALID
    FROM CGENIUS..STK_PVSAIPM_SUB WHERE SEQ=@SEQ


    --定义表变量
    IF OBJECT_ID('TEMPDB..#TAB') IS NOT NULL
    DROP TABLE #TAB
    CREATE TABLE #TAB (
    COLNAME VARCHAR(50) NOT NULL,
    COLVALUE NUMERIC(18,4) NULL
    )

    --定义表变量
    IF OBJECT_ID('TEMPDB..#TMP') IS NOT NULL
    DROP TABLE #TMP
    CREATE TABLE #TMP (
    SEQ INT
    ,ISVALID INT
    ,MTIME DATETIME
    ,COMCODE  INT
    ,RPT_DATE DATETIME
    ,RPT_TYPE  INT
    ,RPT_SRC  INT
    ,DATA_TYPE INT
    ,VALUE NUMERIC(18,4)
    )

    --取目的表所有计算字段,存入到临时表中
    INSERT INTO #TAB(COLNAME)
    SELECT NAME FROM CGENIUS.DBO.SYSCOLUMNS WHERE ID=OBJECT_ID('CGENIUS..STK_ASSET_DEPR') AND NAME!='CTIME'

    INSERT INTO #TMP (COMCODE,RPT_DATE,RPT_TYPE,RPT_SRC,VALUE,DATA_TYPE)
    SELECT COMCODE,RPT_DATE,RPT_TYPE,RPT_SRC,VALUE,DATA_TYPE
    FROM (
    SELECT
    A.COMCODE 
    ,A.RPT_DATE
    ,A.RPT_TYPE 
    ,A.RPT_SRC 
    ,B.START_VAL AS [1]
    ,B.ADD_VAL AS [2]
    ,B.OTH_ADD_VAL AS [7]
    ,B.LES_VAL AS [5]
    ,B.BAC_VAL AS [3]
    ,B.OUT_VAL AS [4]
    ,B.OTH_LES_VAL AS [0]
    ,B.FINA_VAL AS [6]
    FROM CGENIUS..STK_PVSAIPM_MAIN A INNER JOIN CGENIUS..STK_PVSAIPM_SUB B
    ON A.SEQ=B.P_SEQ
    WHERE A.ISVALID=1 AND B.ISVALID=1
    AND B.SEQ=@SEQ
    --AND B.SEQ=2
    )T1 UNPIVOT (VALUE FOR DATA_TYPE IN ([1], [2],[7], [5], [3], [4], [0],[6])
    )AS UNPVT;

    DECLARE @NUM INT ,@MAXSEQ INT
    -------------------------生成SEQ号-------------------------
    UPDATE A
    SET A.SEQ=B.SEQ,ISVALID=@Y_ISVALID,MTIME=GETDATE()
    FROM #TMP A JOIN CGENIUS..STK_ASSET_DEPR B
    ON
    A.COMCODE  =B.COMCODE  AND
    A.RPT_DATE =B.PERIODDATE AND
    A.RPT_TYPE  =B.RPT_TYPE  AND
    A.DATA_TYPE =B.DATA_TYPE

    SET @NUM =( SELECT COUNT(*) FROM #TMP WHERE SEQ IS NULL )

    IF @NUM>=1
    BEGIN
    EXEC PUBDB..GETSEQGROUP 'STK_ASSET_DEPR',@NUM,@MAXSEQ OUTPUT --生成新的SEQ
    ;WITH TT AS (
    SELECT ROW_NUMBER() OVER(ORDER BY DATA_TYPE ) AS R,* FROM #TMP WHERE SEQ IS NULL )
    UPDATE TT SET SEQ=@MAXSEQ-R+1 , ISVALID=@Y_ISVALID, MTIME=GETDATE()
    END

    --------------------------------------------------

    --取字段名
    -- 当ITEM_ID为空时
    --STK_PVSAIPM_SUB.ITEM关联FIN014.F001 WHERE FIN014.F003=3得到FIN014.F002,用其关联FIN015.F002
    -- WHERE FIN015.F001=11得到FIN015.F003。得到的FIN015.F003就是横表的字段英文名;然后把STK_PVSAIPM_SUB对应的值转入
    IF @Y_ITEM_ID IS NULL
    BEGIN

    SET @FILE= (
    SELECT B.F003 FROM CGENIUS.DBO.FIN014 A JOIN CGENIUS.DBO.FIN015 B
    ON A.F002=B.F002
    WHERE A.F001=@Y_ITEM AND A.F003=3 AND B.F001=11
    AND A.ISVALID=1 AND B.ISVALID=1
    )
    END
    ELSE
    BEGIN
    --当ITEM_ID不为空时
    --STK_PVSAIPM_SUB.ITEM_ID关联FIN014.ITEMID WHERE FIN014.F003=3得到FIN014.F002,用其关联FIN015.F002
    -- WHERE FIN015.F001=11得到FIN015.F003。得到的FIN015.F003就是横表的字段英文名;然后把STK_PVSAIPM_SUB对应的值转入

    SET @FILE= (
    SELECT B.F003 FROM CGENIUS.DBO.FIN014 A JOIN CGENIUS.DBO.FIN015 B
    ON A.F002=B.F002
    WHERE A.ITEMID=@Y_ITEM_ID AND a.F003=3 AND B.F001=11
    AND A.ISVALID=1 AND B.ISVALID=1
    )
    END

    IF ( SELECT COUNT(*) FROM #TMP )=0
    BEGIN
    RETURN
    END


    IF @FILE NOT IN (SELECT COLNAME FROM #TAB )---判断新生成记录,是否是目的表中字段
    BEGIN
    RETURN
    END


    ---返回结果集
    DECLARE @SQLSTR NVARCHAR(MAX) --动态SQL字符串

    SET @SQLSTR='
    SELECT
    SEQ AS SEQ
    ,ISVALID AS ISVALID
    ,MTIME AS MTIME
    ,COMCODE AS COMCODE
    ,RPT_DATE AS PERIODDATE
    ,RPT_TYPE AS RPT_TYPE
    ,RPT_SRC AS RPT_SRC
    ,VALUE AS '+ @FILE+ ','+
    'DATA_TYPE AS DATA_TYPE
    FROM #TMP '

    EXECUTE SP_EXECUTESQL @SQLSTR

    END --程序结束了


  • 相关阅读:
    Flink安装排坑指南
    Java对类进行XSS过滤
    java.sql.SQLException: 无法转换为内部表示
    Flutter环境搭建
    运行npm命令 TypeError: Cannot read property 'get' of undefined
    利用Xilinx的cordic ip做开方运算
    modelsim脚本化仿真出现的两个错误
    opencv中使用imread()读入的图像,始终为显示为8位深度
    PS端时钟供给PL使用的问题
    Xilinx 使用shift_RAM消耗分布式RAM(LUTRAM)还是BRAM (Block RAM)?
  • 原文地址:https://www.cnblogs.com/gina11/p/13566203.html
Copyright © 2011-2022 走看看