原代码来自:东莞--小小大神
使用
--聚合函数 SELECT father_key,dbo.String_Agg(department_name) FROM dbo.b_department GROUP BY father_key --首字母大写 SELECT dbo.[Capit](null),dbo.capit('我是 xiaoxiao a') --计算表达式的值 SELECT dbo.Fn_Cacl('1+6*2*(1+1)') --日期时间格式化 SELECT dbo.FormatDateTime(GETDATE(),'yyyy年MM月dd日'), dbo.FormatDateTime(GETDATE(),'yyyy-MM-dd hh:mm:ss'), dbo.FormatDateTime(GETDATE(),'mm:ss'), dbo.FormatDateTime(GETDATE(),'yyyy/M/d'), dbo.FormatDateTime(GETDATE(),'dd/MM/yyyy') --四舍六入五成双 SELECT dbo.MyRound(5.25,1),dbo.MyRound(5.35,1) --字符串处理 SELECT dbo.TrimAll('0124我是在测试789','0123456789'), dbo.PadLeft('我',10,'0'),dbo.PadRight('我',10,'0'), dbo.TrimEnd('0124我是在测试789','0123456789'),dbo.TrimStart('0124我是在测试789','0123456789') ,dbo.ToSBC('wo'),dbo.ToDBC('wo'),dbo.charat('1,2,3,4,5',',',0),dbo.MyTrim(' 000 ') --字符串拆分 SELECT * FROM dbo.String_Split('1,2,3,4',',') --连续数字 SELECT * FROM dbo.Ft_GetNumbers(11) --正则表达式匹配替换 SELECT dbo.Reg_Match('我是12的','d'),dbo.Reg_CharIndex('我是12的','d'),dbo.Reg_Replace('我是12的','d','') --正则表达式拆分 SELECT * FROM dbo.Reg_Split('1A 2B 3C 4D 5E 6F的 7G 8H 9I 10J 11Q 12J 13K 14L 15M 16N ffee80 #800080','d+[A-Z] ') --取第i个逗号分割的字符串 SELECT dbo.CharAt('1,2,3,4,5,6',',',2)
定义,这里是已经编译好的DLL
ALTER DATABASE master SET TRUSTWORTHY ON go CREATE ASSEMBLY [Database2] FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C010300989BE65B0000000000000000E00022200B013000003E00000006000000000000625C000000200000006000000000001000200000000200000400000000000000040000000000000000A000000002000000000000030040850000100000100000000010000010000000000000100000000000000000000000105C00004F000000006000006803000000000000000000000000000000000000008000000C000000D85A00001C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000783C000000200000003E000000020000000000000000000000000000200000602E7273726300000068030000006000000004000000400000000000000000000000000000400000402E72656C6F6300000C0000000080000000020000004400000000000000000000000000004000004200000000000000000000000000000000445C000000000000480000000200050018340000C026000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001B3003002C0000000100001100047201000070510004020328040000065100DE160A00281800000A066F1900000A6F1A00000A0000DE002A01100000000008000D1500160F0000011B3002002B00000001000011000372010000705100030228030000065100DE160A00281800000A066F1900000A6F1A00000A0000DE002A0001100000000008000C1400160F0000011330020077000000020000110002281B00000A74100000010A0672030000706F1C00000A00067E1D00000A6F1E00000A00026F1F00000A720B0000706F2000000A0D092C0F0020C0000000282100000A00002B0A001F30282100000A0000066F2200000A74110000010B076F2300000A732400000A0C086F2500000A13042B0011042A001B300400FA000000030000110072010000700A02281B00000A74100000010B0772190000706F1C00000A000720881300006F2600000A000772230000706F2700000A00077E1D00000A6F1E00000A00026F1F00000A720B0000706F2000000A130511052C0F0020C0000000282100000A00002B0A001F30282100000A0000282800000A036F2900000A0C07088E696A6F2A00000A00076F2B00000A13060011060816088E696F2C00000A0011066F2D00000A0000DE0D11062C0811066F0F00000A00DC076F2200000A74110000010D096F2300000A13041104282800000A732E00000A13070011076F2500000A0A00DE0D11072C0811076F0F00000A00DC0613082B0011082A0000011C00000200910019AA000D000000000200D9000CE5000D000000001B300200A3010000040000110000140A036F1F00000A0C0828290000060D0920B05EF81135240920A0892B062E672B0009201F3F12082E6F2B000920B05EF8113B9800000038130100000920DE5B3B93351A0920618BA9803B8F0000002B000920DE5B3B932E1C38F10000000920F268B4962E572B000920AB6DB4992E3B38DA000000087245000070282F00000A2D6B38C8000000087255000070282F00000A2D6738B6000000087265000070282F00000A2D6338A4000000087271000070282F00000A2D5F389200000008727B000070282F00000A2D5B3880000000087285000070282F00000A2D572B71087291000070282F00000A2D562B6202283000000A733100000A0A2B6202283200000A733100000A0A2B5402283300000A733100000A0A2B4602283400000A733100000A0A2B3802282800000A733100000A0A2B2A02283500000A733100000A0A2B1C02283600000A733100000A0A2B0E02283000000A733100000A0A2B002B0E00281800000A076F1A00000A0000066F3700000A250B14FE03130411042DE1066F3800000A0000DE18130500281800000A11056F1900000A6F1A00000A0000DE002A00411C00000000000001000000890100008A010000180000000F0000012202283900000A002A3E1FFE732C00000625027D0A0000042A0000001330020011000000050000110002A5070000020A03067B03000004542A000000133004003700000006000011000214FE010A062C04140B2B2804283A00000A0C082C0C02031F206F3B00000A0B2B120203046F3C00000A16936F3B00000A0B2B00072A00133004003700000006000011000214FE010A062C04140B2B2804283A00000A0C082C0C02031F206F3D00000A0B2B120203046F3C00000A16936F3D00000A0B2B00072A00133002001800000007000011000214FE010A062C04140B2B09026F3E00000A0B2B00072A133002001E00000007000011000214FE010A062C04140B2B0F02036F3C00000A6F3F00000A0B2B00072A0000133002001E00000007000011000214FE010A062C04140B2B0F02036F3C00000A6F4000000A0B2B00072A0000133002001E00000007000011000214FE010A062C04140B2B0F02036F3C00000A6F4100000A0B2B00072A0000133002002D00000008000011000F00284200000A16FE010A062C0500140B2B17028C0300001B284300000A0C120203284400000A0B2B00072A000000133004007100000009000011000214FE010B072C0914284500000A0C2B5D026F3C00000A0A160D2B38000609931F20FE01130411042C0B00060920003000009D2B1B0609931F7FFE04130511052C0D060906099320E0FE000058D19D000917580D09068E69FE04130611062DBC06734600000A284500000A0C2B00082A00000013300400770000000A000011000214FE010B072C04140C2B68026F3C00000A0A160D2B48000609932000300000FE01130411042C080006091F209D2B2B0609932000FF0000310C060993205FFF0000FE042B0116130511052C0D060906099320E0FE000059D19D000917580D09068E69FE04130611062DAC06734600000A0C2B00082A0013300200190000000B0000110002A5080000020A04067B040000045103067B05000004542A5A1FFE733400000625027D1000000425037D120000042A5A1FFE733C00000625027D1900000425037D1B0000042A00133002002100000006000011000214FE010A062C04140B2B120203284700000A0C1202284800000A0B2B00072A00000013300200300000000C000011000214FE010B072C04150C2B210203284900000A0A066F4A00000A0D092C0B066F4B00000A17580C2B04160C2B00082A1B3006003300000007000011000214FE010A062C04140B2B24000002178D400000012516036F3C00000A16939D6F4C00000A049A0BDE062600140BDE00072A000110000000000E001D2B00060D00000113300300210000000D000011000214FE010B072C04140C2B1203734D00000A0A0602046F4E00000A0C2B00082A00000013300200290000000E000011000214FE010B072C04140C2B1A284F00000A6F5000000A6F5100000A0A06026F5200000A0C2B00082A000000133002001E0000000F000011000F00285300000A0F01285400000A285500000A285600000A0A2B00062A00001B3002005A000000100000110072B300007002285700000A0A72C9000070735800000A0B00076F5900000A0006285A00000A0C0006285A00000A07735B00000A0D096F5C00000A6F5D00000A1304DE13130500141304DE0B072C07076F0F00000A00DC11042A0000011C0000000027001D4400080F00000102001800344C000B00000000360002735E00000A7D010000042A0000133002002A00000011000011000F01285F00000A0A062C03002B1A027B010000040F01286000000A6F6100000A1F2C6F6200000A262A5200027B01000004037B010000046F6300000A262A00133004004B00000012000011007E6400000A0A027B010000042C10027B010000046F6500000A16FE022B01160B072C1C00027B0100000416027B010000046F6500000A17596F6600000A0A0006736700000A0C2B00082A4E0002036F6800000A736900000A7D010000042A520003027B010000046F5D00000A6F6A00000A002A133004008700000013000011000F00285F00000A2D120F01285F00000A2D090F02285F00000A2B01170B072C1200281800000A72F90000706F1A00000A000020FEFF00000A0F03286B00000A2D1A0F03286C00000A20FFFF00002F0C0F03286C00000A16FE022B01160C082C090F03286C00000AD10A0F00286000000A0F01286000000A0F02286000000A062825000006002A001B30060051010000140000110003286D00000A16FE010A062C090003286E00000A260072C9000070735800000A0B00076F5900000A00076F6F00000A0C0008026F7000000A00086F7100000A0D00161304161305287200000A1306281800000A720D0100706F1A00000A002B7300721D0100700304110425175813048C49000001287300000A130709051107282700000613081107737400000A287500000A130911051108581305281800000A723D010070110711088C490000011209726F010070287600000A287300000A6F1A00000A00110805FE04130A110A2C022B060017130B2B88287200000A1106591306281800000A727F0100706F1A00000A00281800000A728F0100706F1A00000A00281800000A729F01007011058C4900000111068C49000001287700000A6F1A00000A0000DE0B092C07096F0F00000A00DC00DE0B082C07086F0F00000A00DC00DE0B072C07076F0F00000A00DC2A000000414C00000200000041000000E8000000290100000B00000000000000020000003100000006010000370100000B00000000000000020000002200000023010000450100000B0000000000000013300400900100001500001100036F7800000A6F7900000A6F5D00000A0A060B0728290000060C0820B2F1B6473542082052F23329351D08207D455E093B870000002B00082052F233293B9E0000003836010000082097B00E2A3BDC0000002B000820B2F1B6473BB1000000381901000008203E8D8CC2351A0820E55124653BA80000002B0008203E8D8CC22E4D38F700000008203FEFAAC72E642B000820517FAFD32E692B000820BDFD94FD2E0538D60000000772C3010070282F00000A3A8600000038C10000000772D9010070282F00000A2D7438AF0000000772E9010070282F00000A2D62389D0000000772FF010070282F00000A2D50388B000000077213020070282F00000A2D3E2B7C077225020070282F00000A2D2F2B6D077237020070282F00000A2D202B5E077249020070282F00000A2D112B4F07725B020070282F00000A2D022B40036F5D00000A6F1F00000A726F020070282F00000A0D092C11020405036F5D00000A6F49000006002B14020405036F5D00000A287A00000A6F48000006002B11020405036F5D00000A6F49000006002B002A1B300500BB0000001600001100030A0473440000060B076F460000060000160C2B170007160802086F7B00000A6F490000060000081758D10C08026F7C00000AFE040D092DDC1713042B5100026F7D00000A16FE01130511052C0800110417590A2B471613062B1C00070211066F7E00000A110411062826000006000011061758D113061106026F7C00000AFE04130711072DD40011041758D11304110403FE0216FE01130811082DA100DE0613090011097ADE0A00076F470000060000DC06130A2B00110A2A00011C00000000110090A100060F0000010200110098A9000A00000000133002002E00000017000011022C2920C59D1C810A160B2B1402076F7F00000A066120930100015A0A0717580B07026F8000000A2F022BE1062A260002037D030000042A420002037D0500000402047D040000042A7E02283900000A0002037D0600000402284F00000A6F8100000A7D080000042A062A00133003007800000018000011027B060000040A062C082B0006172E042B042B042B33162A02157D060000040002177D0B0000042B380002027B0B000004732A0000068C070000027D0700000402177D06000004172A02157D0600000400027B0B0000040B020717587D0B000004027B0B000004027B09000004FE0216FE010C082DB3162A1E027B070000042A1A738200000A7A00133002003C00000019000011027B060000041FFE331D027B08000004284F00000A6F8100000A330B02167D06000004020A2B0716732C0000060A06027B0A0000047D09000004062A1E0228320000062A7E02283900000A0002037D0C00000402284F00000A6F8100000A7D0E0000042A13300400B90000001A000011027B0C0000040A062C082B0006172E042B042B042B71162A02157D0C00000400027B0F00000414FE030B0739870000000002177D1300000402027B0F000004027B11000004284900000A7D140000042B4F0002027B130000040C020817587D1300000408027B140000046F8300000A732B0000068C080000027D0D00000402177D0C000004172A02157D0C00000402027B140000046F8400000A7D1400000400027B140000046F4A00000A0D092DA20002147D14000004162A1E027B0D0000042A00000013300200480000001B000011027B0C0000041FFE331D027B0E000004284F00000A6F8100000A330B02167D0C000004020A2B071673340000060A06027B100000047D0F00000406027B120000047D11000004062A1E02283A0000062A7E02283900000A0002037D1500000402284F00000A6F8100000A7D170000042A13300700E70000001C000011027B150000040A062C082B0006172E042B072B073897000000162A02157D1500000400027B1800000414FE030B0739B20000000002177D1C0000040002027B18000004178D400000012516027B1A0000046F3C00000A16939D6F4C00000A7D1D00000402167D1E0000042B6102027B1D000004027B1E0000049A7D1F0000040002027B1C0000040C020817587D1C00000408027B1F000004732B0000068C080000027D1600000402177D15000004172A02157D150000040002147D1F00000402027B1E00000417587D1E000004027B1E000004027B1D0000048E69328F02147D1D00000400162A1E027B160000042A0013300200480000001D000011027B150000041FFE331D027B17000004284F00000A6F8100000A330B02167D15000004020A2B0716733C0000060A06027B190000047D1800000406027B1B0000047D1A000004062A1E0228420000062A5A02283900000A000002031A738500000A7D200000042A0013300400310000001E0000110000030A160B2B220607930C0008288600000A0D027B200000040916098E696F2C00000A00000717580B07068E6932D82A6A00021C8D5200000125D002000004288700000A2845000006002A820002188D5200000125161F0A9D284500000600027B200000046F2D00000A002A000000133005003C0000001F00001100021B8D52000001251620030200009D25171F0E9D2518039D2519049D28450000060005288800000A0A027B200000040616068E696F2C00000A002A133006004C0000001F00001100283000000A056F2900000A0A021C8D52000001251620040200009D2517068E691E58D19D2518039D2519049D251B068E69D19D284500000600027B200000040616068E696F2C00000A002A42534A4201000100000000000C00000076322E302E35303732370000000005006C00000050100000237E0000BC100000B40D000023537472696E677300000000701E00007C02000023555300EC200000100000002347554944000000FC200000C405000023426C6F6200000000000000020000015797A22B0902000000FA01330016000001000000550000000D00000020000000490000005500000010000000880000003C000000010000001F0000000300000006000000060000001500000003000000010000000100000003000000070000000000ED0601000000000006005305030A0600C005030A06005004F0090F00230A000006006404D10706003605D10706000205D1070600A705D10706007305D10706008C05D1070600B204D1070600E504D10706001A0B65070A007B044A0906000A0865070E00EC0C210B0E00BD03210B0600C1086B0106005E076B010A0021054A090600AD029F0A0600270065070600300365070A005406320A0E007D06800A0E00670D800A06005008BC070A00C502320A0A001201320A0A0036024A090A00F007A70B0A001F02A70B06005303650706003504030A0600B902650706006D099F0A060040009A01060032009A010600CD04F0090A000E0B4A090A0091044A090A0007064A090600ED08260D0600D9086B01060021096B010A007500320A0A00A508A70B060037076B010A00420D4A090A004B034A090E00F00C210B0E00B007210B0600B40765070600600665070E00FB08210B0E005D03210B0E00C103210B0600CE086B0106003406260D0600E40C650706007A0765070E006F08800A0E00A903800A0600790865070600EE01230606003508BC070600950665070A00E30714080A000702140806008F0D6B01060059086B010600BD0B650706007800650706002C086B010600C000650706008503650706004108D1070A00980814080600FE07650706006B026B0106003509650706000B0165070600E50A030A0600830D65070600D8026507000000002D01000000000100010001001000470A000035000100010001001000B20A00003500010007000120100018060000350001001D00010010008A03000035000200240000010000360100003500020029000B011000A7020000850003002A000B01100091000000850004002B000301100013000000350006002C0003011000AF00000035000C00340003011000F7000000350015003C000200100015090000350020004400130100004E000000850021004A0001009301BB023301C600C00206002A08C4020600050BDC010600D306C40201001604C40201009B0CC7020100C901C40201002A08C40206007307C40201000B00C40201001604C40201009B0CC7020100C901C40201000C0DDC010600070DDC0101006C08DC0106006708DC0101000B00C40201007E00CA0201001604C40201009B0CC7020100C901C40201000C0DDC010600070DDC0101006C08DC0106006708DC0101000B00C40201008A00CE020100A800C4020100EF00DC0101004209D20250200000000096008C07D702010098200000000096008207DF020400E0200000000096002407B001060064210000000096002B07E60207008822000000009600120DEC0209005424000000008618EA0906000B005D24000000009600D70AF2020B007024000000009100FB06F8020C009024000000009600510BFF020E00D424000000009600590BFF02110018250000000096006C07B00114003C25000000009600E506E60215006825000000009600DA0CE60217009425000000009600F501E6021900C0250000000096002A0306031B00FC250000000096005B0129011D007C260000000096005501B0011E000027000000009100A20011031F0025270000000096006E0B1A0322003C27000000009600780B1A03240054270000000096006706E60226008427000000009600590D21032800C027000000009600070B27032A00102800000000960058022E032D0040280000000096008A0BB00130007828000000009600450235033100A428000000009600DD06B00133005424000000008618EA09060034002829000000008600850B06003400382900000000860001043E0334006E29000000008600A1024403350084290000000086000C044A033600DB2900000000E601DE014F033600EF2900000000E6012F04560337005424000000008618EA0906003800042A000000009600680A5D033800982A000000009100760A6A033C00442C000000009100150B72034000E02D0000000091002F047B0344005424000000008618EA0906004700C42E000000009300830684034700FE2E000000008618EA0901004800082F000000008618EA0989034900192F000000008618EA0901004B00392F00000000E101DF0306004C003C2F00000000E1011D0D15004C00C02F00000000E1092E0C25004C00C82F00000000E1012C0B06004C00C02F00000000E109700C25004C00D02F00000000E10179098F034C00183000000000E101BD093A004C002030000000008618EA0901004C00392F00000000E101DF0306004D00403000000000E1011D0D15004D00053100000000E1092E0C25004D00C82F00000000E1012C0B06004D00053100000000E109700C25004D00103100000000E10179098F034D00643100000000E101BD093A004D006C31000000008618EA0901004D00392F00000000E101DF0306004E008C3100000000E1011D0D15004E007F3200000000E1092E0C25004E00C82F00000000E1012C0B06004E007F3200000000E109700C25004E00883200000000E10179098F034E00DC3200000000E101BD093A004E00E432000000008618EA0910004E00FC32000000008100EB0298034F0039330000000086002A0406005000543300000000860021040600500078330000000086008C089E035000C0330000000086004806A5035300000001003307000002002708020003009C0B000001003307020002009C0B00000100330700000100330700000200810100000100F602000002003606000001002A08000001006402020002002A08000001000C0D00000200D30600000300B301000001000C0D00000200D30600000300B301000001000C0D000001000C0D00000200D10A000001000C0D00000200D10A000001000C0D00000200D10A000001000C0D00000200A30B000001000C0D000001000C0D00000100640202000200D30602000300050B000001000C0D000002006C08000001000C0D000002006C08000001000C0D000002006C08000001000C0D000002006C08000001000C0D00000200D10A00000300D306000001000C0D000002006C08000003006B0D000001000C0D00000100D906000002009601000001007508000001000106000001000F0900000100EE09000001004D0D000001002007000002009A0600000300040300000400B70C000001002007000002009A0600000300040300000400B70C000001002E0900000200D506000003006B0D00000400B10D00000100E60800000200D40C000003000E0300000100050B000001002A0800000100D30600000200050B00000100160400000100160400000100160400000100A30600000100610A000001006B0D00000200B10D000003000106000001006B0D00000200B10D0000030001060400A90009000A00090055000900060009008D00090091000A000A000A0055000A0006000A008D000A0091000B000A000B0055000B0006000B008D000B0091000900EA0901001100EA0906001900EA090A002900EA0910003100EA0910003900EA0910004100EA0910004900EA0910005100EA0910005900EA0910006100EA0910007100EA090600A100EA0906001101EA0906001901F203060021011D0D15000C008F0C200021014B0B060021018F0C25001400DC093000A900DC093A003901EA0906004901EA094000890142034C0079009502520091012A0210009901FA03610099014D021000A1010100680081009C076D00B10165095200B101C8067400B9010B0779009901CD038000C901420786009100EA098B00D101FD0152009901FB0C010099017A031000D9012401A300D901580AA9009901B606AF0099015407860099002F04B4009900D90306009100EA09BC00B101990DD000D901900BA3009100EA09D600D9017402A300D9016B00A300D9011B01A300D9016101A300D9018002A300D10139035200D101D90306006900EA090600B101A50DE900B101510BEE00B1017D0DF400B101590BEE00B1016E075200B1016E07FE00B101DA0CFE00B101F501FE001C00F4051500E1011F031201B9005E061801C100620B2901B101EA092F01D1007106D000E9015E065200D1007D064D01F101F40A1500F9014F0D5401B1017F0B5801D100EA091000D1005C0266010902E301730109029603790111024C087F01D900B1031801E100DE058901E900DE058D01190247029101E100620B9701B1010E0BAA01F900EA091000210297070600E1015E06B0010101EA09B50129027E08250069005E0652005901EA090600C10000071500C100DE05520059012F02C00159012F02C70159012F02CE01B101AD0DDC015901AB06540159015E06DF01C100EA09100061013D0652005901EA09100069012F0410007101000715007101DE0554013102000BE9003102890DFF01F900110206022902320D10000101B3080C024102C60C1202B1010E0B16025102EA0910005102AB061E0259025E061801B1010E0B22026900720330026902FB025200E101CF023602710217034B027102A80C54017102DE0115007901E8055002B101C70A5A02B101AB0654010902B50154017902EA090600F901DE055200C900790671028101EA0986028902580A970299026D0D9D028902580AAC022000630002042E000B00B0032E001300B9032E001B00D8032E002300E1032E002B00E1032E003300EF032E003B00E1032E004300E1032E004B00E1032E005300E1032E005B00F5034000630002048300BB005F05A00063000204C30073000204E0006B00070420016B00020423017300020440016B00020443017300020460016B00020463017300020480016B000204A0016B000204C0016B000204E0016B00020400026B00020420026B00020460026B003F0480026B008A04A0026B000204C0026B000204E0026B00020400036B00020420036B00020440036B00020460036B00CE048004630002048005B3000204A005B3000204E005B30002040006B30002042006B30002044006B30002046006B30002048006B3000204A006B3000204E006B30002040007B30002042007B30002044007B30002046007B30002048007B3000204A007B3000204E007B30002040008B30002042008B30002044008B30002046008B300020401000C0000000D00470056009100C500DE00E300F90004011D013501400145015F016C0184019D01BC01D501E501EB0129023B0255025F0265026A0276027B0281028E02A702090001000A0003000B0005000000C90BAC030000070CAC030000C90BAC030000070CAC030000C90BAC030000070CAC0302002F00030002003100050002003700070002003900090002003F000B00020041000D0009005A001F0009005C00210009005E002300090060002500090062002700090064002900090066002B000A006A001F000A006C0021000A006E0023000A00700025000A00720027000A00740029000A0076002B000B007A001F000B007C0021000B007E0023000B00800025000B00820027000B00840029000B0086002B00190029000B01685C0000020004800000010000000000010000000000000098000000020000000000000000000000B2028A0100000000020000000000000000000000B202750100000000020000000000000000000000B2026507000000000700030008000300090003000A0003000B0003000C0005000D000600000000000056657273696F6E3130003C693E355F5F31003C46745F4765744E756D626572733E645F5F31004E756C6C61626C6560310049456E756D657261626C6560310049456E756D657261746F726031005F5F5374617469634172726179496E69745479706553697A653D3132006765745F55544633320053716C496E743332003C6D617463683E355F5F32003C3E735F5F32005461626C6532004461746162617365320046696C6C32003C3E735F5F33003C5265675F53706C69743E645F5F313400496E7436340046423833424542323345353342413637374634413934324246423431393538373737313137463834003C733E355F5F34003C537472696E675F53706C69743E645F5F31350055496E7431360053716C496E743136006765745F55544637006765745F55544638003C4D6F64756C653E003C50726976617465496D706C656D656E746174696F6E44657461696C733E00546F44424300546F534243006765745F41534349490053797374656D2E494F0053797374656D2E4461746100706F737444617461006D73636F726C6962007362006465630053797374656D2E436F6C6C656374696F6E732E47656E65726963006765745F4D616E616765645468726561644964003C3E6C5F5F696E697469616C54687265616449640052656164006765745F43757272656E74546872656164005472696D456E640052656164546F456E64004462436F6D6D616E6400437265617465436F6D6D616E640053716C436F6D6D616E640053656E6400417070656E6400446174614163636573734B696E64004D79526F756E64007365745F4D6574686F64005265675F5265706C61636500736F757263650046696C654D6F6465006765745F556E69636F6465006765745F426967456E6469616E556E69636F6465006765745F4D657373616765004D65726765005461626C650049456E756D657261626C650049446973706F7361626C650053716C446F75626C6500546F446F75626C650052756E74696D654669656C6448616E646C65005F777269746546696C650066696C65006765745F4E616D65007461626C654E616D650066696C654E616D65004765744E616D6500546F4461746554696D6500466F726D61744461746554696D6500526561644C696E65006765745F506970650053716C506970650056616C75655479706500536563757269747950726F746F636F6C547970650047657454797065007365745F436F6E74656E7454797065006D7950726F636564757265006765745F43757272656E7443756C74757265004361707475726500546F5469746C65436173650048747470576562526573706F6E736500476574526573706F6E736500436C6F73650053797374656D2E49446973706F7361626C652E446973706F73650043726561746500416363756D756C617465005465726D696E617465003C3E315F5F737461746500456E64577269746500426567696E577269746500436F6D70696C657247656E6572617465644174747269627574650044656275676761626C6541747472696275746500417373656D626C795469746C654174747269627574650053716C50726F6365647572654174747269627574650053716C55736572446566696E656441676772656761746541747472696275746500417373656D626C7954726164656D61726B41747472696275746500446562756767657248696464656E41747472696275746500417373656D626C7946696C6556657273696F6E41747472696275746500417373656D626C79436F6E66696775726174696F6E4174747269627574650053716C46756E6374696F6E41747472696275746500417373656D626C794465736372697074696F6E41747472696275746500436F6D70696C6174696F6E52656C61786174696F6E7341747472696275746500417373656D626C7950726F6475637441747472696275746500417373656D626C79436F7079726967687441747472696275746500417373656D626C79436F6D70616E794174747269627574650052756E74696D65436F6D7061746962696C697479417474726962757465006765745F56616C75650047657453716C56616C7565006765745F48617356616C75650076616C7565004942696E61727953657269616C697A6500537472696E675F4167670053797374656D2E546872656164696E6700456E636F64696E670052656164537472696E67005772697465537472696E670053716C537472696E6700546F537472696E67005265675F4D617463680049734D61746368004E6578744D6174636800436F6D70757465537472696E6748617368004D6174680073617665506174680073747250617468006765745F4C656E677468007365745F436F6E74656E744C656E67746800537461727473576974680069006F626A0076616C00466E5F4361636C005472696D416C6C004461746162617365322E646C6C0046696C6C006765745F49734E756C6C007365745F536563757269747950726F746F636F6C0073716C0047657455726C00506F737455726C0075726C0046696C6553747265616D00476574526573706F6E736553747265616D004765745265717565737453747265616D0053797374656D004D795472696D003C3E335F5F6E00426F6F6C65616E0073705F6765746D616E0073705F706F73746D616E004F70656E007365745F50726F746F636F6C56657273696F6E004874747056657273696F6E0053797374656D2E476C6F62616C697A6174696F6E0053797374656D2E5265666C656374696F6E004462436F6E6E656374696F6E0053716C436F6E6E656374696F6E004E6F74537570706F72746564457863657074696F6E0053797374656D2E446174612E436F6D6D6F6E006A736F6E0046696C65496E666F0043756C74757265496E666F004D656D626572496E666F006765745F54657874496E666F004469726563746F7279496E666F003C3E335F5F73700047726F757000657870004368617200457865637574655363616C61720057726974654E756D626572004462446174615265616465720053716C4461746152656164657200457865637574655265616465720053747265616D52656164657200546578745265616465720042696E6172795265616465720072656164657200537472696E674275696C6465720053657276696365506F696E744D616E61676572006F7468657200457863656C5772697465720042696E6172795772697465720077726974657200426974436F6E766572746572005F776972746572004D6963726F736F66742E53716C5365727665722E53657276657200546F4C6F7765720049456E756D657261746F720053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261626C653C53797374656D2E4F626A6563743E2E476574456E756D657261746F720053797374656D2E436F6C6C656374696F6E732E49456E756D657261626C652E476574456E756D657261746F72002E63746F720053797374656D2E446961676E6F73746963730053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300446562756767696E674D6F6465730053797374656D2E446174612E53716C54797065730053746F72656450726F636564757265730047657442797465730076616C7565730042756C6B436F7079546F586C73004578706F7274586C730053797374656D2E546578742E526567756C617245787072657373696F6E730053797374656D2E436F6C6C656374696F6E730055736572446566696E656446756E6374696F6E73006765745F43686172730063686172730046745F4765744E756D626572730052756E74696D6548656C70657273006765745F53756363657373004578697374730043686172417400466F726D61740057726974654F626A6563740053797374656D2E4E65740053797374656D2E436F6C6C656374696F6E732E49456E756D657261746F722E5265736574005061644C656674005061645269676874006F705F496D706C69636974005265675F53706C697400537472696E675F53706C697400496E6974004361706974006765745F44656661756C7400726573756C7400666D740053797374656D2E446174612E53716C436C69656E7400456E7669726F6E6D656E740053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261746F723C53797374656D2E4F626A6563743E2E43757272656E740053797374656D2E436F6C6C656374696F6E732E49456E756D657261746F722E43757272656E740053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261746F723C53797374656D2E4F626A6563743E2E6765745F43757272656E740053797374656D2E436F6C6C656374696F6E732E49456E756D657261746F722E6765745F43757272656E74003C3E325F5F63757272656E74006765745F4669656C64436F756E74006D61785265636F7264436F756E74006765745F5469636B436F756E7400636F756E74005472696D537461727400436F6E76657274004874747057656252657175657374007365745F54696D656F7574003C3E335F5F696E7075740073705F52656164547874004D6F76654E6578740053797374656D2E54657874007365745F436F6D6D616E64546578740053716C436F6E746578740077006765745F496E646578005265675F43686172496E64657800526567657800496E697469616C697A65417272617900546F436861724172726179004372656174654469726563746F7279006F705F457175616C6974790049734E756C6C4F72456D707479000000010007470045005400000D680074007400700073003A00000950004F005300540000216100700070006C00690063006100740069006F006E002F006A0073006F006E00000F640065006600610075006C007400000F75006E00690063006F0064006500000B750074006600330032000009750074006600370000097500740066003800000B61007300630069006900002162006900670065006E006400690061006E0075006E00690063006F00640065000015730065006C0065006300740020007B0030007D00002F63006F006E007400650078007400200063006F006E006E0065006300740069006F006E003D00740072007500650000132000938F6551E14F6F600D4E8C5B746501FF010F2000005FCB59FC5BFA5170656E63011F7B0030007D002F007B0031007D005F007B0032007D002E0078006C007300003120008765F64E7B0030007D002C00200071517B0031007D00200061672C00200027590F5C7B0032007D002000575B8282010F2300230023002C00230023002300000F2000FC5BFA5170656E638C5B1062010F2D002D002D002D002D002D002D000123200071517B0030007D002000616770656E630CFF1780F6657B0031007D006D0073000115530071006C0042006F006F006C00650061006E00000F530071006C0042007900740065000015530071006C0044006500630069006D0061006C000013530071006C0044006F00750062006C0065000011530071006C0049006E007400310036000011530071006C0049006E007400330032000011530071006C0049006E007400360034000011530071006C004D006F006E00650079000013530071006C00530069006E0067006C00650000096E0075006C006C0000000000CBCE7E549884A64F9CFA9414C34D174E00042001010803200001052001011111042001010E032000020615128095011C04200013000320001C0615128099011C09200015128095011300052000128091062001011180A1040701123D0500001280C90320000E0A0705124112451249020E0600011280CD0E04061280D5062001011280D5042001020E060001011180E10520001280E5042000124D05200101124D1107090E12411D051245124D02124D12490E0500001280ED0520011D050E042001010A072003011D05080808200201124D1280ED0A070612490E0E0902123D050002020E0E072002010E1280ED040701111C050703020E02040001020E0520020E08030420001D03040702020E0520010E1D03060703020E115D0615115901115D050001115D1C0420010E0E0B07071D030211610802020205000111610E052001011D030A07071D03020E080202020407011120070704126502080206000212650E0E032000080620011D0E1D030607031269020E0520020E0E0E060703126D020E050000128105052000128109042000126D04070111710320000D032000060500020D0D0805000111710D0C07060E127D0E1280810E123D0500020E0E1C0400010E0E062002010E127D030701020620011280AD0E0620011280AD030620011280AD1C0607030E02116102060E0520020E080805070307020213070C02127D1280811280BD0808080E080A020206000112811D0E0520001280810520001280BD030000080700040E0E1C1C1C0320000A0600030E0E1C1C0607040E0E09020520001281310400010D0E0F070B08123007020702070202123D080420010E080420011C080407020908042001030805070308080204070112240607040802080204200012650407011228050703080208040701122C072002010E1181410807041D0708071D050500011D0507090002011281511181550407011D050500011D050D08B77A5C561934E08904061280AD0306113402060802061C0306126503061D0E04061280C1070003010E0E100E060002010E100E0500020E0E0E050002010E0E050001125508060002011C10080600030E0E080E0A00020E15115901115D0E080003011C1008100E06000212550E0E050002080E0E0600030E0E0E080600030E0E0E0E0800021171117111750520010111610520010112100420001161062001011280B1062001011280B50C0004011161116111611180B9070004010E0E0E070800040112301C0707080003081280BD070E040001090E05200201080E08200015128095011C052001011D070620030107070D0620030107070E0328001C0801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F7773010801000701000000000D0100084945E5BC80E58F9100000501000000000C010007312E302E302E31000004010000003701000200540E1146696C6C526F774D6574686F644E616D650446696C6C540E0F5461626C65446566696E6974696F6E0756616C20696E744A01000200540E1146696C6C526F774D6574686F644E616D650546696C6C32540E0F5461626C65446566696E6974696F6E19696420696E742C56616C206E766172636861722834303030294301000200540E1146696C6C526F774D6574686F644E616D650546696C6C32540E0F5461626C65446566696E6974696F6E1256616C206E76617263686172283430303029808F010001005455794D6963726F736F66742E53716C5365727665722E5365727665722E446174614163636573734B696E642C2053797374656D2E446174612C2056657273696F6E3D322E302E302E302C2043756C747572653D6E65757472616C2C205075626C69634B6579546F6B656E3D623737613563353631393334653038390A44617461416363657373010000006101000200000004005402124973496E76617269616E74546F4E756C6C73015402174973496E76617269616E74546F4475706C696361746573005402124973496E76617269616E74546F4F726465720054080B4D61784279746553697A65401F000000000000000000989BE65B00000000020000001C010000F45A0000F43C000052534453DA76129B1D60CD4FA4EBFFE5F3726DCC01000000443A5C4749545C4461746142617365325C4461746162617365325C6F626A5C44656275675C4461746162617365322E706462000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000385C00000000000000000000525C0000002000000000000000000000000000000000000000000000445C0000000000000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF2500200010090808000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000018000080000000000000000000000000000001000100000030000080000000000000000000000000000001000000000048000000586000000C03000000000000000000000C0334000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000001000100000000000100010000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B0046C020000010053007400720069006E006700460069006C00650049006E0066006F00000048020000010030003000300030003000340062003000000022000500010043006F006D006D0065006E0074007300000049004500005FD153000000002A000500010043006F006D00700061006E0079004E0061006D0065000000000049004500005FD15300000000320005000100460069006C0065004400650073006300720069007000740069006F006E000000000049004500005FD15300000000300008000100460069006C006500560065007200730069006F006E000000000031002E0030002E0030002E00310000003C000E00010049006E007400650072006E0061006C004E0061006D00650000004400610074006100620061007300650032002E0064006C006C0000002E00050001004C006500670061006C0043006F007000790072006900670068007400000049004500005FD153000000003200050001004C006500670061006C00540072006100640065006D00610072006B0073000000000049004500005FD1530000000044000E0001004F0072006900670069006E0061006C00460069006C0065006E0061006D00650000004400610074006100620061007300650032002E0064006C006C0000002A0005000100500072006F0064007500630074004E0061006D0065000000000049004500005FD15300000000340008000100500072006F006400750063007400560065007200730069006F006E00000031002E0030002E0030002E003100000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000031002E0030002E0030002E0031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005000000C000000643C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 WITH PERMISSION_SET = UNSAFE GO /* --报错 消息 10327,级别 14,状态 1,第 1 行 针对程序集 'Database2' 的 CREATE ASSEMBLY 失败,因为程序集 'Database2' 未获授权, 不满足 PERMISSION_SET = UNSAFE。满足以下两个条件之一时将给程序集授权: 数据库所有者(DBO)拥有 UNSAFE ASSEMBLY 权限,且数据库具有 TRUSTWORTHY 数据库属性; 或者,程序集已使用其对应登录名具有 UNSAFE ASSEMBLY 权限的证书或非对称密钥加以签名。 --解决 ALTER DATABASE master SET TRUSTWORTHY ON go */ exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'clr enabled',1 reconfigure exec sp_configure 'show advanced options',0 RECONFIGURE go /* DROP AGGREGATE String_Agg DROP FUNCTION Capit DROP FUNCTION Fn_Cacl DROP FUNCTION FormatDateTime DROP FUNCTION MyRound DROP FUNCTION MyTrim DROP FUNCTION PadLeft DROP FUNCTION PadRight DROP FUNCTION Reg_CharIndex DROP FUNCTION Reg_Match DROP FUNCTION Reg_Replace DROP FUNCTION ToDBC DROP FUNCTION ToSBC DROP FUNCTION TrimAll DROP FUNCTION TrimEnd DROP FUNCTION TrimStart DROP FUNCTION Ft_GetNumbers DROP FUNCTION Reg_Split DROP FUNCTION String_Split DROP PROCEDURE BulkCopyToXls DROP PROCEDURE sp_getman DROP PROCEDURE sp_postman DROP PROCEDURE sp_ReadTxt DROP FUNCTION CharAt DROP FUNCTION ft_clearing DROP ASSEMBLY database2 */ --CLR CREATE AGGREGATE [dbo].[String_Agg](@value NVARCHAR (4000)) RETURNS NVARCHAR (4000) EXTERNAL NAME [Database2].[String_Agg] go CREATE FUNCTION [dbo].[Capit] (@input NVARCHAR (MAX)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[Capit] GO CREATE FUNCTION [dbo].[Fn_Cacl] (@exp NVARCHAR (1000)) RETURNS NVARCHAR (1000) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[Fn_Cacl] GO CREATE FUNCTION [dbo].[FormatDateTime] (@input DATETIME, @fmt NVARCHAR (200)) RETURNS NVARCHAR (200) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[FormatDateTime] GO CREATE FUNCTION [dbo].[MyRound] (@val FLOAT (53), @dec SMALLINT) RETURNS FLOAT (53) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[MyRound] GO CREATE FUNCTION [dbo].[MyTrim] (@input NVARCHAR (MAX)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[MyTrim] GO CREATE FUNCTION [dbo].[PadLeft] (@input NVARCHAR (MAX), @i INT, @c NVARCHAR (1000)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[PadLeft] GO CREATE FUNCTION [dbo].[PadRight] (@input NVARCHAR (MAX), @i INT, @c NVARCHAR (1000)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[PadRight] GO CREATE FUNCTION [dbo].[Reg_CharIndex] (@input NVARCHAR (MAX), @sp NVARCHAR (1000)) RETURNS INT AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[Reg_CharIndex] GO CREATE FUNCTION [dbo].[Reg_Match] (@input NVARCHAR (MAX), @sp NVARCHAR (1000)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[Reg_Match] GO CREATE FUNCTION [dbo].[Reg_Replace] (@input NVARCHAR (MAX), @sp NVARCHAR (1000), @x NVARCHAR (1000)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[Reg_Replace] Go /* CREATE FUNCTION [dbo].[ft_clearing] (@input NVARCHAR (MAX)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[ft_clearing] GO */ CREATE FUNCTION [dbo].[ToDBC] (@input NVARCHAR (MAX)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[ToDBC] GO CREATE FUNCTION [dbo].[ToSBC] (@input NVARCHAR (MAX)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[ToSBC] GO CREATE FUNCTION [dbo].[TrimAll] (@input NVARCHAR (MAX), @chars NVARCHAR (100)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[TrimAll] GO CREATE FUNCTION [dbo].[TrimEnd] (@input NVARCHAR (MAX), @chars NVARCHAR (100)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[TrimEnd] GO CREATE FUNCTION [dbo].[TrimStart] (@input NVARCHAR (MAX), @chars NVARCHAR (100)) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[TrimStart] GO CREATE FUNCTION [dbo].[Ft_GetNumbers] (@n INT) RETURNS TABLE ( [Val] INT NULL) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[Ft_GetNumbers] GO CREATE FUNCTION [dbo].[Reg_Split] (@input NVARCHAR (MAX), @sp NVARCHAR (1000)) RETURNS TABLE ( [id] INT, [Val] NVARCHAR (4000) NULL) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[Reg_Split] GO CREATE FUNCTION [dbo].[String_Split] (@input NVARCHAR (MAX), @sp NVARCHAR (1)) RETURNS TABLE ( [id] INT, [Val] NVARCHAR (4000) NULL) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[String_Split] GO CREATE PROCEDURE [dbo].[BulkCopyToXls] @sql NVARCHAR (4000), @savePath NVARCHAR (1000), @tableName NVARCHAR (200), @maxRecordCount INT AS EXTERNAL NAME [Database2].[myProcedure].[BulkCopyToXls] GO CREATE PROCEDURE [dbo].[sp_getman] @url NVARCHAR (1000), @result NVARCHAR (MAX) OUTPUT AS EXTERNAL NAME [Database2].[StoredProcedures].[sp_getman] GO CREATE PROCEDURE [dbo].[sp_postman] @url NVARCHAR (1000), @json NVARCHAR (4000), @result NVARCHAR (MAX) OUTPUT AS EXTERNAL NAME [Database2].[StoredProcedures].[sp_postman] GO CREATE PROCEDURE [dbo].[sp_ReadTxt] @file NVARCHAR (1000), @coding NVARCHAR (100) AS EXTERNAL NAME [Database2].[StoredProcedures].[sp_ReadTxt] GO CREATE FUNCTION [dbo].[CharAt] (@input NVARCHAR (MAX), @chars NVARCHAR (100),@i int) RETURNS NVARCHAR (MAX) AS EXTERNAL NAME [Database2].[UserDefinedFunctions].[CharAt]
C#源码
--1、 sql function 1 using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Collections; using System.Text.RegularExpressions; using System.Globalization; using System.Threading; using System.Text; public partial class UserDefinedFunctions { /* 表值函数,生成N条记录,测试用,速度不错哦 */ struct Table { public int n; public Table(int n) { this.n = n; } } [SqlFunction(FillRowMethodName = "Fill", TableDefinition = "Val int")] public static IEnumerable Ft_GetNumbers(int n) { for (int i = 1; i <= n; i++) { yield return new Table(i); } } private static void Fill(object source, out int n) { Table pi = (Table)source; n = pi.n; } [SqlFunction] public static string PadLeft(string input, int i, string c) { if (input == null) return null; //字符补齐 if (string.IsNullOrEmpty(c)) return input.PadLeft(i, ' '); else return input.PadLeft(i, c.ToCharArray()[0]); } [SqlFunction] public static string PadRight(string input, int i, string c) { if (input == null) return null; //字符补齐 if (string.IsNullOrEmpty(c)) return input.PadRight(i, ' '); else return input.PadRight(i, c.ToCharArray()[0]); } [SqlFunction] public static string MyTrim(string input ) { if (input == null) return null; return input.Trim( ); } [SqlFunction] public static string TrimAll(string input,string chars) { if (input == null) return null; return input.Trim(chars.ToCharArray()); } [SqlFunction] public static string TrimStart(string input, string chars) { if (input == null) return null; return input.TrimStart(chars.ToCharArray()); } [SqlFunction] public static string TrimEnd(string input, string chars) { if (input == null) return null; return input.TrimEnd(chars.ToCharArray()); } [SqlFunction] public static string FormatDateTime(DateTime? input, string fmt ) { if (input==null) { return null; } return Convert.ToDateTime(input).ToString(fmt); } [SqlFunction] public static SqlString ToSBC(string input) { if (input == null) return null; //半角转全角: char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 32) { c[i] = (char)12288; continue; } if (c[i] < 127) c[i] = (char)(c[i] + 65248); } return new string(c); } [SqlFunction] public static string ToDBC(string input) { if (input == null) return null; //全角转半角: char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); } return new string(c); } /* 表值函数,字符串拆分,这个有用 */ struct Table2 { public string s; public int i; public Table2(int i, string s) { this.i=i; this.s = s; } } private static void Fill2(object source,out int i, out string s) { Table2 T = (Table2)source; s = T.s; i = T.i; } /* 表值函数,字符提取,可以写正则表达式匹配和替换的函数,有用 */ [SqlFunction(FillRowMethodName = "Fill2", TableDefinition = "id int,Val nvarchar(4000)")] public static IEnumerable Reg_Split(string input, string sp) { if (input != null) { int i = 1; Match match = Regex.Match(input, sp); while (match.Success) { yield return new Table2(i++,match.Value); match = match.NextMatch(); } } } [SqlFunction(FillRowMethodName = "Fill2", TableDefinition = "Val nvarchar(4000)")] public static IEnumerable String_Split(string input, string sp) { if (input != null) { int i = 1; foreach (string s in input.Split(sp.ToCharArray()[0])) { yield return new Table2(i++,s); } } } [SqlFunction] public static string Reg_Match(string input, string sp) { if (input == null) return null; return Regex.IsMatch(input, sp).ToString(); } [SqlFunction] public static int Reg_CharIndex(string input, string sp) { if (input == null) return -1; Match match = Regex.Match(input, sp); if (match.Success) return match.Index + 1; return 0; } [SqlFunction] public static string CharAt(string input, string chars, int i) { if (input == null) return null; try { return input.Split(chars.ToCharArray()[0])[i]; } catch { return null; } } [SqlFunction] public static string Reg_Replace(string input, string sp, string x) { if (input == null) return null; Regex rx = new Regex(sp); return rx.Replace(input, x); } [SqlFunction] public static string ft_clearing(string input) { if (input == null) return ""; Regex rx = new Regex(@"s"); return rx.Replace(input, "").Trim(); } [SqlFunction] public static string Capit(string input) { if (input == null) return null; //首字母大写,很有用,速度非常好,神速 TextInfo tx = Thread.CurrentThread.CurrentCulture.TextInfo; return tx.ToTitleCase(input); } [Microsoft.SqlServer.Server.SqlFunction] public static SqlDouble MyRound(SqlDouble val, SqlInt16 dec) { return Math.Round( val.Value, dec.Value); } [SqlFunction(DataAccess = DataAccessKind.Read)] public static string Fn_Cacl(string exp) { string sql = string.Format("select {0}", exp); using (SqlConnection cn = new SqlConnection("context connection=true")) { cn.Open(); string select = Convert.ToString(sql); try { SqlCommand cmd = new SqlCommand(Convert.ToString(sql), cn); return cmd.ExecuteScalar().ToString(); } catch (Exception ex) { return null; } } } } -- 2、SqlAggregate1.cs using System; using System.Data; using Microsoft.SqlServer.Server; using System.Data.SqlTypes; using System.IO; using System.Text; [Serializable] [SqlUserDefinedAggregate(Format.UserDefined, IsInvariantToNulls = true, IsInvariantToDuplicates = false, IsInvariantToOrder = false, MaxByteSize = 8000)] public class String_Agg : IBinarySerialize { //字符串拼接,这个有用 private StringBuilder sb; public void Init() { this.sb = new StringBuilder(); } public void Accumulate(SqlString value) { if (value.IsNull) { return; } this.sb.Append(value.Value).Append(','); } public void Merge(String_Agg other) { this.sb.Append(other.sb); } public SqlString Terminate() { string output = string.Empty; if (this.sb != null && this.sb.Length > 0) { output = this.sb.ToString(0, this.sb.Length - 1); } return new SqlString(output); } public void Read(BinaryReader r) { sb = new StringBuilder(r.ReadString()); } public void Write(BinaryWriter w) { w.Write(this.sb.ToString()); } } --3、SqlStoredProcedure1.cs using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Net; using System.IO; using System.Text; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void sp_postman(string url,string json,out string result) { result = ""; try { result = PostUrl(url, json); } catch (Exception ex) { SqlContext.Pipe.Send(ex.Message); } } [Microsoft.SqlServer.Server.SqlProcedure] public static void sp_getman(string url, out string result) { result = ""; try { result = GetUrl(url); } catch (Exception ex) { SqlContext.Pipe.Send(ex.Message); } } public static string GetUrl(string url ) { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "GET"; req.ProtocolVersion = HttpVersion.Version10; if (url.ToLower().StartsWith("https:")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } else { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; } HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader rd = new StreamReader(res.GetResponseStream()); return rd.ReadToEnd(); } public static string PostUrl(string url, string postData) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.Timeout = 5000; req.ContentType = "application/json"; req.ProtocolVersion = HttpVersion.Version10; if (url.ToLower().StartsWith("https:")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } else { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; } byte[] data = Encoding.UTF8.GetBytes(postData); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; } [Microsoft.SqlServer.Server.SqlProcedure] public static void sp_ReadTxt(string file,string coding) { try { StreamReader sr = null; switch (coding.ToLower()) { case "default": sr = new StreamReader(file, System.Text.Encoding.Default); break; case "unicode": sr = new StreamReader(file, System.Text.Encoding.Unicode); break; case "utf32": sr = new StreamReader(file, System.Text.Encoding.UTF32); break; case "utf7": sr = new StreamReader(file, System.Text.Encoding.UTF7); break; case "utf8": sr = new StreamReader(file, System.Text.Encoding.UTF8); break; case "ascii": sr = new StreamReader(file, System.Text.Encoding.ASCII); break; case "bigendianunicode": sr = new StreamReader(file, System.Text.Encoding.BigEndianUnicode); break; default: sr = new StreamReader(file, System.Text.Encoding.Default); break; } string line; while ((line = sr.ReadLine()) != null) { SqlContext.Pipe.Send(line); } sr.Close(); } catch (Exception ex) { SqlContext.Pipe.Send(ex.Message); } } } --4、SqlStoredProcedure2.cs using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; public partial class myProcedure { [Microsoft.SqlServer.Server.SqlProcedure] public static void BulkCopyToXls(SqlString sql, SqlString savePath, SqlString tableName, SqlInt32 maxRecordCount) { if (sql.IsNull || savePath.IsNull || tableName.IsNull) { SqlContext.Pipe.Send(" 输入信息不完整!"); } //每个excel文件最大容纳65534 ushort _maxRecordCount = ushort.MaxValue - 1; if (maxRecordCount.IsNull == false && maxRecordCount.Value < ushort.MaxValue && maxRecordCount.Value > 0) _maxRecordCount = (ushort)maxRecordCount.Value; ExportXls(sql.Value, savePath.Value, tableName.Value, _maxRecordCount); } private static void ExportXls(string sql, string savePath, string tableName, System.UInt16 maxRecordCount) { //创建文件路径 if (System.IO.Directory.Exists(savePath) == false) { System.IO.Directory.CreateDirectory(savePath); } using (SqlConnection conn = new SqlConnection("context connection=true")) { conn.Open(); using (SqlCommand command = conn.CreateCommand()) { command.CommandText = sql; using (SqlDataReader reader = command.ExecuteReader()) { int i = 0; int totalCount = 0; int tick = System.Environment.TickCount; SqlContext.Pipe.Send(" 开始导出数据"); while (true) { string fileName = string.Format(@"{0}/{1}_{2}.xls", savePath, tableName, i++); int iExp = Write(reader, maxRecordCount, fileName); long size = new System.IO.FileInfo(fileName).Length; totalCount += iExp; SqlContext.Pipe.Send(string.Format(" 文件{0}, 共{1} 条, 大小{2} 字节", fileName, iExp, size.ToString("###,###"))); if (iExp < maxRecordCount) break; } tick = System.Environment.TickCount - tick; SqlContext.Pipe.Send(" 导出数据完成"); SqlContext.Pipe.Send("-------"); SqlContext.Pipe.Send(string.Format(" 共{0} 条数据,耗时{1}ms", totalCount, tick)); } } } } private static void WriteObject(ExcelWriter writer, object obj, System.UInt16 x, System.UInt16 y) { //判断写数字还是写字符 string type = obj.GetType().Name.ToString(); switch (type) { case "SqlBoolean": case "SqlByte": case "SqlDecimal": case "SqlDouble": case "SqlInt16": case "SqlInt32": case "SqlInt64": case "SqlMoney": case "SqlSingle": if (obj.ToString().ToLower() == "null") writer.WriteString(x, y, obj.ToString()); else writer.WriteNumber(x, y, Convert.ToDouble(obj.ToString())); break; default: writer.WriteString(x, y, obj.ToString()); break; } } private static int Write(SqlDataReader reader, System.UInt16 count, string fileName) { int iExp = count; ExcelWriter writer = new ExcelWriter(fileName); writer.BeginWrite(); try { //写字段信息 for (System.UInt16 j = 0; j < reader.FieldCount; j++) { writer.WriteString(0, j, reader.GetName(j)); } //循环一行一行读入数据 for (System.UInt16 i = 1; i <= count; i++) { if (reader.Read() == false) { iExp = i - 1; break; } //循环一格一格写入数据 for (System.UInt16 j = 0; j < reader.FieldCount; j++) { WriteObject(writer, reader.GetSqlValue(j), i, j); } } } catch (Exception ex) { throw ex; } finally { writer.EndWrite(); } return iExp; } public class ExcelWriter { System.IO.FileStream _wirter; //创建文件 public ExcelWriter(string strPath) { _wirter = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate); } //写数组 private void _writeFile(System.UInt16[] values) { foreach (System.UInt16 v in values) { byte[] b = System.BitConverter.GetBytes(v); _wirter.Write(b, 0, b.Length); } } //写文件头 public void BeginWrite() { _writeFile(new System.UInt16[] { 0x809, 8, 0, 0x10, 0, 0 }); } //文件尾 public void EndWrite() { _writeFile(new System.UInt16[] { 0xa, 0 }); _wirter.Close(); } //写数字到单元格 public void WriteNumber(System.UInt16 x, System.UInt16 y, double value) { _writeFile(new System.UInt16[] { 0x203, 14, x, y, 0 }); byte[] b = System.BitConverter.GetBytes(value); _wirter.Write(b, 0, b.Length); } //写字符到单元格 public void WriteString(System.UInt16 x, System.UInt16 y, string value) { byte[] b = System.Text.Encoding.Default.GetBytes(value); _writeFile(new System.UInt16[] { 0x204, (System.UInt16)(b.Length + 8), x, y, 0, (System.UInt16)b.Length }); _wirter.Write(b, 0, b.Length); } } };