2017覯 伎企 string_agg 襯 郁, 2017 覩碁 覯企 朱 clr襯 襷れ.
CREATE ASSEMBLY [string_sum]
FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C010300A26376500000000000000000E00002210B010800001400000006000000000000DE3200000020000000400000000040000020000000020000040000000000000004000000000000000080000000020000000000000200408500001000001000000000100000100000000000001000000000000000000000008C3200004F00000000400000B002000000000000000000000000000000000000006000000C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000E4120000002000000014000000020000000000000000000000000000200000602E72737263000000B0020000004000000004000000160000000000000000000000000000400000402E72656C6F6300000C0000000060000000020000001A00000000000000000000000000004000004200000000000000000000000000000000C0320000000000004800000002000500C8220000C40F000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001E02280100000A2A1E02280400000A2AA6730600000A8001000004730700000A8002000004730800000A8003000004730900000A80040000042A0000133001000F000000010000117E010000046F0A00000A0A2B00062A00133001000F000000020000117E020000046F0B00000A0A2B00062A00133001000F000000030000117E030000046F0C00000A0A2B00062A00133001000F000000040000117E040000046F0D00000A0A2B00062A001330020011000000050000110203281100000A281200000A0A2B00062A000000133001000B0000000600001102281300000A0A2B00062A00133001000F00000007000011D005000002281400000A0A2B00062A00133001000B0000000800001102281500000A0A2B00062A00133001001800000009000011028C0100001B2D0A280100002B0A2B062B04020A2B00062A13300200100000000A000011031200FE150200001B06810200001B2A1E02281700000A2A133002002C0000000B000011027B1900000A6F1A00000A0B078C0300001B2D12280200002B0B027B1900000A076F1B00000A070A2B00062A4A02281700000A02731C00000A7D1900000A2A001E02281700000A2A3202732000000A7D060000042A0000009A0F01282100000A2C022B1A027B060000040F01282200000A6F2300000A1F2C6F2400000A262A004E027B06000004037B060000046F2500000A262A13300400440000000C0000117E2600000A0A027B060000042C28027B060000046F2700000A16311A027B0600000416027B060000046F2700000A17DA6F2800000A0A120206282900000A080B2B00072A4A02036F2A00000A732B00000A7D060000042A004A03027B060000046F2C00000A6F2D00000A2A0042534A4201000100000000000C00000076322E302E35303732370000000005006C00000030060000237E00009C0600002406000023537472696E677300000000C00C00000800000023555300C80C0000100000002347554944000000D80C0000EC02000023426C6F6200000000000000020000015717A20B090E000000FA013300160000010000001D000000070000000600000017000000070000000100000031000000270000000C00000002000000050000000500000002000000090000000100000004000000020000000300000002000000000012060100000000000A00AE0084000A00E200C4000600F200EB000A004A0184000600C801EB000A004A0220020E0096027B020600B302A7020E00EE02D902060029031F0306003D031F031200680352031200810352031200AE0396030600D803C5030A001704F0030A002F0413001200640447040600990479040600B704EB000600DB04EB000A00F404130006004205230506006805EB000E008B057B020E00AC057B020600B305EB000600C90579040600E905790400000000010000000000010001000000000029003700050001000100000000003A00370009000100020000011000450037000D0001000300050100004F0000000D0005000800050100005D0000000D0005000F0001200000780000000D000600110031000D012000310036012D00310058013A0031007D0147002100590290000100C1029D005020000000000618BE00130001005820000000000618BE00130001006020000000001118F900170001008C2000000000130800011B000100A820000000001308260128000100C4200000000013084F0135000100E0200000000013086D0142000100FC20000000004602B301630001001C21000000004602BC01680002003421000000008300CD016C0002005021000000004602D501710002006821000000001100DE01780002008C21000000000100FC0180000300A821000000000618BE0013000400B02100000000030810028B000400E821000000000618BE0013000400FC21000000000618BE00130004000422000000000600D402130004001422000000000600F802A10004003C220000000006000903A700050050220000000006001503AD000600A0220000000066033603B2000600B4220000000066034A03B800070000000100BA0100000100F30100000100F301000001000303000001000F03000001003B0300000100500307001D000900BE0013006100BE00BE007100BE00CD001100BE0013007900BE0013002400BE0013002C00BE0013003400BE0013003C00BE001300240010028B002C0010028B00340010028B003C0010028B008100BE0013008900BE0013009100BE0020019900A8046B011900B30163001900BC0168002900C90478011900D5017100A900E50488011900BE001300B100BE0098014400590290004C000F058B004C00190515024C00BE001300B900BE00220239003603B20039004A03B8004100BE001300490056052D0249000F057100410061053102410061053702410061053D02C1006F0543024100750568004100D50146024900BE0020015100800571004100BE0020014100D501710059004A032001C900BE005402D900BE001300E100BE00BC02E900BE0013002900830025012E008B01CA022E008301C10240002B00EB0040001300C40043001300C40043001B00D30049008300430163001300C40063001B00D30069008300360180002B00EB0083001B00D30083007300EB0083007B00EB00890083005701A0002B00EB00A3001300C400A300C300A001C0002B00EB00C3001300C400C300EB002702E0002B00EB00E30073015A0200012B00EB0000011300C40020012B00EB0020011300C40040012B00EB0040011300C40060012B00EB0060011300C40080012B00EB00A0012B00EB00C0011300C400C0012B00EB00E0012B00EB0000021300C40000022B00EB000C01110116011B01700174017F018401930193011B024C0204000100060005000000E2004F0000009B01540000004A0159000000A7015E0000006302980002000400030002000500050002000600070002000700090002000F000B0007002C003D0007002E003F00750075008800F000F700FE000501020209020480000000000000000000000000000000000706000002000000000000000000000001000A00000000000800000000000000000000000A0013000000000002000000000000000000000001006F02000000000200000000000000000000000100EB00000000000500040006000400000010000C00F101000010001900F101000000001B00F1012D008E012D0010020000003C4D6F64756C653E006D73636F726C6962004D6963726F736F66742E56697375616C4261736963004D794170706C69636174696F6E004D79004D79436F6D7075746572004D7950726F6A656374004D79576562536572766963657300546872656164536166654F626A65637450726F7669646572603100436F6E636174656E617465004D6963726F736F66742E56697375616C42617369632E4170706C69636174696F6E5365727669636573004170706C69636174696F6E42617365002E63746F72004D6963726F736F66742E56697375616C42617369632E4465766963657300436F6D70757465720053797374656D004F626A656374002E6363746F72006765745F436F6D7075746572006D5F436F6D70757465724F626A65637450726F7669646572006765745F4170706C69636174696F6E006D5F4170704F626A65637450726F76696465720055736572006765745F55736572006D5F557365724F626A65637450726F7669646572006765745F5765625365727669636573006D5F4D7957656253657276696365734F626A65637450726F7669646572004170706C69636174696F6E00576562536572766963657300457175616C73006F0047657448617368436F64650054797065004765745479706500546F537472696E67004372656174655F5F496E7374616E63655F5F005400696E7374616E636500446973706F73655F5F496E7374616E63655F5F006765745F476574496E7374616E6365004D6963726F736F66742E56697375616C42617369632E4D7953657276696365732E496E7465726E616C00436F6E7465787456616C75656031006D5F436F6E7465787400476574496E7374616E63650053797374656D2E44617461004D6963726F736F66742E53716C5365727665722E536572766572004942696E61727953657269616C697A650053797374656D2E5465787400537472696E674275696C64657200696E7465726D656469617465526573756C7400496E69740053797374656D2E446174612E53716C54797065730053716C537472696E6700416363756D756C6174650076616C7565004D65726765006F74686572005465726D696E6174650053797374656D2E494F0042696E617279526561646572005265616400720042696E61727957726974657200577269746500770053797374656D2E436F6D706F6E656E744D6F64656C00456469746F7242726F777361626C6541747472696275746500456469746F7242726F777361626C6553746174650053797374656D2E436F6465446F6D2E436F6D70696C65720047656E657261746564436F64654174747269627574650053797374656D2E446961676E6F737469637300446562756767657248696464656E417474726962757465004D6963726F736F66742E56697375616C42617369632E436F6D70696C65725365727669636573005374616E646172644D6F64756C6541747472696275746500486964654D6F64756C654E616D654174747269627574650053797374656D2E436F6D706F6E656E744D6F64656C2E44657369676E0048656C704B6579776F72644174747269627574650053797374656D2E52756E74696D652E436F6D70696C657253657276696365730052756E74696D6548656C70657273004765744F626A65637456616C75650052756E74696D655479706548616E646C65004765745479706546726F6D48616E646C6500416374697661746F7200437265617465496E7374616E6365004D7947726F7570436F6C6C656374696F6E417474726962757465006765745F56616C7565007365745F56616C75650053797374656D2E52756E74696D652E496E7465726F70536572766963657300436F6D56697369626C65417474726962757465006765745F49734E756C6C00417070656E6400537472696E6700456D707479006765745F4C656E6774680052656164537472696E670053716C55736572446566696E656441676772656761746541747472696275746500466F726D61740053657269616C697A61626C6541747472696275746500436F6D70696C6174696F6E52656C61786174696F6E734174747269627574650052756E74696D65436F6D7061746962696C69747941747472696275746500737472696E675F73756D00737472696E675F73756D2E646C6C000000000003200000000000DDCF5A8B42C7714C8C1CA2C8F5975C6E0008B77A5C561934E08908B03F5F7F11D50A3A0320000103000001040000120C070615121801120C040000120807061512180112080400001211070615121801121104000012140706151218011214040800120C040800120804080012110408001214042001021C0320000804200012150320000E021E00071001011E001E000730010101101E000213000420001300070615121901130004280013000306122105200101112505200101121C042000112505200101122905200101122D052001011135080100010000000000052002010E0E1701000A4D7954656D706C61746507382E302E302E30000004010000000615121801120C061512180112080615121801121106151218011214040701120C040701120804070112110407011214042001010E1001000B4D792E436F6D707574657200000C0100074D792E5573657200001301000E4D792E4170706C69636174696F6E00001301000E4D792E576562536572766963657300000400011C1C03070102030701080600011215115104070112150307010E051001001E00040A011E000407011E00072004010E0E0E0E6101003453797374656D2E5765622E53657276696365732E50726F746F636F6C732E536F617048747470436C69656E7450726F746F636F6C124372656174655F5F496E7374616E63655F5F13446973706F73655F5F496E7374616E63655F5F0000000615121801130006151219011300040A0113000520010113000607021300130004200101020501000000000320000205200112210E05200112210305200112211C02060E0520020E08080707030E112511250520010111696101000200000004005402124973496E76617269616E74546F4E756C6C73015402174973496E76617269616E74546F4475706C696361746573005402124973496E76617269616E74546F4F726465720054080B4D61784279746553697A65FFFFFFFF04200101080801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F777301000000B43200000000000000000000CE320000002000000000000000000000000000000000000000000000C0320000000000000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF2500204000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100100000001800008000000000000000000000000000000100010000003000008000000000000000000000000000000100000000004800000058400000540200000000000000000000540234000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000000000000000000000000000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004B4010000010053007400720069006E006700460069006C00650049006E0066006F0000009001000001003000300030003000300034006200300000002C0002000100460069006C0065004400650073006300720069007000740069006F006E000000000020000000300008000100460069006C006500560065007200730069006F006E000000000030002E0030002E0030002E003000000040000F00010049006E007400650072006E0061006C004E0061006D006500000073007400720069006E0067005F00730075006D002E0064006C006C00000000002800020001004C006500670061006C0043006F00700079007200690067006800740000002000000048000F0001004F0072006900670069006E0061006C00460069006C0065006E0061006D006500000073007400720069006E0067005F00730075006D002E0064006C006C0000000000340008000100500072006F006400750063007400560065007200730069006F006E00000030002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000030002E0030002E0030002E003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000C000000E03200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
WITH PERMISSION_SET = SAFE
GO

CREATE AGGREGATE [dbo].[string_sum]
(@input [nvarchar](4000))
RETURNS[nvarchar](max)
EXTERNAL NAME [string_sum].[Concatenate]
GO

declare 
    @cmd varchar(8000) 

declare cur cursor for
select
    'exec xp_cmdshell ''' + replace(cmd, '''', '''''') + ''', no_output' cmd
from (
    select
        concat('bcp "select ', column_name1, ' union all select top 100 ', column_name2, 'from ', table_name, ' with (nolock)" queryout c:\temp\', file_name, ' -T -w') cmd
    from (
        select
            concat('[', table_catalog, '].[', table_schema, '].[', table_name, ']') table_name
        ,   dbo.string_sum('''' + column_name + '''', ',') column_name1
        ,   dbo.string_sum('convert(nvarchar(500), [' + column_name + '])', ',') column_name2
        ,   concat(table_catalog, '_', table_schema, '_', table_name, '.csv') file_name
        from information_schema.columns 
        where concat(table_catalog, '.', table_schema, '.', table_name) in (select concat(table_catalog, '.', table_schema, '.', table_name) from information_schema.tables where table_type = 'BASE TABLE')
        group by
            concat('[', table_catalog, '].[', table_schema, '].[', table_name, ']')
        ,   concat(table_catalog, '_', table_schema, '_', table_name, '.csv')
    ) t
) t
option(maxdop 1)
open cur;
fetch next from cur into @cmd;
while @@FETCH_STATUS not in (-1, -2)
begin
    exec(@cmd)
	raiserror ('%s', 0, 1, @cmd) with nowait;
	fetch next from cur into @cmd;
end

close cur;
deallocate cur;