• Sql 列转行


    
    

    set statistics io on
    set statistics time ON


    --
    主从表可用 SELECT vcaa, vcbb, ISNULL(( SELECT vcCN + ',' FROM TableB b WHERE a.vcaa= b.vcaa FOR XML PATH('') ), '') AS vcNameList FROM TableA a --单表 SET @vcResult =(SELECT vcID+ ',' FROM table_A WHERE vcSerialNo = @vcOrderID FOR XML PATH(''))

    SELECT
    vcList = (stuff((select ',' + vcId from tb_Table where vcAAA = 123 AND vcBBB = 'abc' for xml path('')),1,1,''))
    
    
    --varchar型
    DECLARE 
         @nTempFinAmount VARCHAR(2000)= '''a;b;c;d;e;f''',
                    @str1 VARCHAR(8000) 
                    IF object_id(N'tempdb..#temp1',N'U') is not null
                    BEGIN
                        DROP TABLE #temp1
                    END
                    CREATE  TABLE #temp1  (
                        id INT,
                        Amount varchar(50)
                    )
    
                    SET @str1 =  'select  Ratio='+replace(@nTempFinAmount,';',''+''' union all select ''')+'' 
                    SET @str1=' select id=identity(int,1,1),Ratio  into #temp from ('+@str1+') a ;
                    insert into #temp1 select * from #temp;'
                    EXEC(@str1)    
                    SELECT  * from #temp1
    
    --int型 DECLARE @str1 VARCHAR(8000) IF object_id(N'tempdb..#temp1',N'U') is not null BEGIN DROP TABLE #temp1 END CREATE TABLE #temp1 ( id INT, Amount DECIMAL(18,2) ) SET @str1 = 'select Ratio='+replace(@nTempFinAmount,',',''+' union all select ')+'' SET @str1=' select id=identity(int,1,1),Ratio into #temp from ('+@str1+') a ; insert into #temp1 select * from #temp;' EXEC(@str1)
  • 相关阅读:
    占位
    阳光服务平台-敏捷开发
    两种方法实现带验证码的用户登录
    红警大战JAVA简单版
    JPanel与JFrame的区别
    java中import详解
    敏捷开发
    GitHub:本地项目上传与团队协作
    从结缘计算机到未来规划
    (三)微信小程序首页的分类功能和搜索功能的实现笔记
  • 原文地址:https://www.cnblogs.com/ghelement/p/6656590.html
Copyright © 2020-2023  润新知