当前位置: 代码迷 >> Sql Server >> f_Split()分割函数有关问题
  详细解决方案

f_Split()分割函数有关问题

热度:126   发布时间:2016-04-24 18:28:45.0
f_Split()分割函数问题
这个函数很多页面都使用过,没出现大问题。
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[f_Split]

(

 @SplitString ntext, -- 如果要传入NText类型,下面需要相应的修改,注释行为NText下同

 @Separator nvarchar(2) = ','-- NVarChar(2) = N','

)

RETURNS @SplitStringsTable TABLE

(

 [id] int identity(1,1),

 [value] nvarchar(4000) -- NVarChar(4000)

)

AS

BEGIN

    DECLARE @CurrentIndex int;

    DECLARE @NextIndex int;

    DECLARE @ReturnText nvarchar(4000);-- NVarChar(4000)

    SELECT @CurrentIndex=1;

    WHILE(@CurrentIndex<=datalength(@SplitString)) -- DATALENGTH(@SplitString)/2

    BEGIN

        SELECT @NextIndex=charindex(@Separator,@SplitString,@CurrentIndex);

        IF(@NextIndex=0 OR @NextIndex IS NULL)

            SELECT @NextIndex=datalength(@SplitString)+1;--DATALENGTH(@SplitString)/2
        

        SELECT @ReturnText=substring(@SplitString,@CurrentIndex,@NextIndex-@CurrentIndex);

        INSERT INTO @SplitStringsTable([value])

        VALUES(@ReturnText);       

        SELECT @CurrentIndex=@NextIndex+1;

    END

    RETURN;

END


select * from dbo.f_Split('11910BE3-FDC8-4F42-B6B9-3DF55938FCF1,E9221AF7-CFB8-428F-9C2F-D5D6A7A815D6,8E7A9B4C-0823-4402-982E-48D9AB51783D,B5180191-AE4A-440D-8BAB-54BC3B8AE15C,F145B0D0-13E4-45EF-8299-8ED88689F397,6FB9FA54-D7BB-46F5-829E-4A3488B6A756,34E7A8F5-2ECE-4FBC-B465-AB52697AF1E8,B735FFA6-C6D5-43C2-8B34-D7FDEED99CED,409753B4-DE91-4957-B3E7-9FD12230C3C5,ED9B7030-0784-462B-9D68-FD0A576A5462,D0FA63CB-0CDF-41BB-9034-49BFA1075C13,B4491941-E272-4866-AA4B-33E78B553FBE,89F34774-7364-4FE7-BA98-34899682A949,DB52F8C5-EED3-451B-A451-E4F08A278122,D397CE8D-C2A0-45B7-A14C-480DEC363ABC,1097AEA2-1870-4C0C-B360-0AD57A307C0F,12C0DD0A-CB2D-45B2-BC4B-793257F1F18F,AB674966-275E-49F8-A388-352C24BE3911,B126F4C3-61CD-4C53-ACDA-F78A7E90F3AA,54BEAFBC-3F32-41ED-AC76-9566857CCC7A,6ACB8C93-FDA5-447B-A200-75B510F43BB9,B83A7B11-7CB6-4FAB-850B-47E4E0E1CE3D,508B7B1E-39D7-42DA-A442-74BF21CEFE32,4A4FEB81-9097-46D1-9C23-2D3B0A145AE6,ABEA5B7C-63F2-494E-AD2B-EC4F74AB59C5,A7B20E81-036C-476F-A589-35C6D1C24907,7BE08E80-4C0B-49FD-9378-BE4174CD68AF,2B9F4EBD-FC04-4783-B744-75FDEB21D37B,0B51708A-ECAE-4733-8722-7DC1565C3AA3,5B21E4A5-4D2C-482A-9455-6181B1A34309,013FA80B-1DED-4CC1-A429-C86FFEF02985,3C6F001D-9197-4787-BEC4-1E1C9FE99C2B,ABA23765-C445-46DA-8208-BF6F97E9B9C4,CF50E0E5-85AC-4AE6-9474-01E7D2B60A08,AA305AF6-17EE-4388-B106-3F882ED17A28,DEBBC7E9-7301-45A9-B9E6-B6F00B2C86ED,EF486A74-31F5-4759-A9D4-B99785E8ACD1,DED99940-FCF7-41E0-9849-6A1AA540CD74,65EDDC64-3A5C-4809-BD06-4358BA051069,4C6DFDC1-2CEA-4D08-AEDB-C41EAE415EA8,BBB0370B-9563-421B-BBC9-42F231197DBC,3D759F0D-D981-4248-B5DF-CDE0810DBA6E,0DE9298E-CA79-4277-9547-9D506508880C,05238619-F986-4728-A006-0F57E913398C,F787F494-E3BB-47C0-AF2E-6064BE393FDD,EC4E6F13-4BA5-4DAE-9F92-B33B02FAFBFB,88A4A566-21E5-4CEC-BC9B-D0CEC840DB3A,72E7AE9C-6CF3-4012-B682-EED1AF0E6D48,F5BBADAB-759F-478F-BA1C-BF251F10E713,EB0EB458-CBD3-478E-B160-5EDEF5031E5A,1EF9E204-9B91-4887-85C9-B40236A06F2A,0FA879BF-E707-4054-8DAC-F44D3F0BCBED,7589BA9E-65B9-476D-9DD2-C0F459F4428C,3D39DDC8-FF28-44A6-BA8C-C5EA66986B08,AF8B8BCB-BF64-4762-8089-5F4045506214,9523019F-1116-4D20-8CE0-4D69A23CE434,EB0C7CB3-4047-4E73-80DF-7B9A68EA01F7,1A0796A3-4DD7-4E72-A684-A0020C0017F7,B1673801-1B6B-4E87-A6FA-A1D55843A42B,C4FF3B18-0C97-4830-950C-572A8F9D2A60,DEF6812D-304A-4FCC-A9D8-84BCD0CECAC2,41453326-74AB-4368-8AD0-2C04597BA0DA,6B3B8D6A-0FEB-4C4C-8339-DF2A3CEFEC5B,FFA726D8-340B-4708-BB9E-4ADA978336D9,FED5E514-A38B-4D9F-9DD9-687907EFE3A3,23CBEEAB-E75C-44DD-98D3-C83041BCBE6D,C16FC07A-0218-4174-B64E-941968EDBD69,1AA632D2-29BF-43C5-83A8-EBC05C01842C,E472EA8F-2134-4543-909B-2F975DD5663A,EE5EEAF9-F465-44B2-985B-EAD87107C088,B694FBED-88A5-4E21-B992-2091BC0D4760,E4527119-5247-4DDB-B6DC-F06DDC89E5FB,3CC87AD2-3C01-45FB-87B1-596AB3B7D4E0,849B8C0E-9E3C-40F8-B97B-91FE607DF243,452D9345-78AD-4C44-AACA-BBA97BC883FA,95A34AE0-F28F-469C-8F33-1576ED7FB295,915CEE49-90B2-4285-B429-8DE05A3A71C2,C288BE49-74AE-4463-B2BF-23A6D50EBF34,92DC10F3-9D91-4E83-9F6C-66225C35F060,14AD3657-ACE2-47A0-A500-30AB4FF1196E,898A843E-4F8A-4651-962B-CA75D33E8A02,0D58A605-7809-468D-91B8-0357E12B7C31,0751C5E1-15EE-439B-A66F-7145D16791E0,B06BBC9D-5457-4086-8E70-7E3130984756,12F9374D-7B94-46F0-ACDC-62765A9DD2B8,A2B95680-E08B-4295-8355-3D48D65916D0,565A4D98-1366-4CC2-A725-019F0251528A,1A791AEE-8199-4AF7-A3D2-F68F0F92E2F1,4651D22E-4388-4808-B329-E8370974CFA4,B9249780-AC95-4760-B9C6-B2710A85D2F2,86366A54-5D06-4E88-8AA0-EE5F24CF080A,6D3D6316-1259-4AA6-8A9C-D42B7BE1D316,FDBEA01B-3C28-4F77-AA05-A8ADE1520DC2,F53BF37E-5167-4324-ABE1-7F8FB7051DAB,0A751E65-563F-4357-B7C9-5EE3FDF534E9,E36FA61E-4017-4988-8BDD-AFDEA012115F,2B61B28A-0D0C-4ECB-8EC7-E5CC46523549,188DDFE2-6394-4577-91DA-B9357DBB52EA,1F12C18A-7CF1-495A-96DD-9B3C4B3CCD7F,7C717054-2C7E-4159-8661-A6C7710A25D7,3BA8170E-AFFE-4806-9701-5F127DA60B20,222E8F19-7F5C-47B1-B878-13BAB04F8BA0,2DCE244E-817D-489D-BE27-5195F3119BD8,04682ACF-FAC0-4E4B-8D13-6B6E8A0EE48A,48E0456F-DE7D-4642-AA02-6E22AF521575,93A8383A-334E-42ED-A023-E096A7F1796C,4A09A9E6-7CF4-4C61-B22D-DB3F774F2293,DA8F15D3-64E8-4CB5-BEB4-8A9EF21B7320,F2DD8940-CB12-4EAE-AB7A-F2E6E26F2CDF,159E278D-30C9-492D-8346-171141AD0EC9,8BF881CB-0678-4532-BF51-28DA4F63934B,90EEFF83-400D-4B50-A844-3A6284901241,E676056D-EE60-4832-8863-868921782908,AC6B8D9A-7111-489C-A27B-3DDA27F336D1,3C60C361-ECF2-46E3-8B77-A1B055D282E1,B054A96A-9540-4DEF-B3D3-224CE1326526,1064C712-8D30-47B9-A7DB-30B01458DDB9,1146759F-32E2-491F-AEB9-FF2BBB94722B,E856A212-663D-478E-89D3-1491E76578F0,36C576C8-3851-430F-AD4B-356C2A68CBD3,7C3B8511-66AA-40F0-939A-9C60CA0C3CE1,DB0C7119-8C19-4D15-A5AF-7D4F62906A6D,D61778BB-D42E-4076-9156-627181CB1228,06790B76-0581-4F28-AD76-C93113C415C1,28C1D62B-BB41-4278-8B00-E51291EBE5DA,C5A5196F-2B5A-479D-874B-33D76717970D,70422C53-1D68-4CFF-9AD7-F0486AE74916,BF3521F0-1A38-4D13-9C13-9EB187EF724C,75657EB9-CD2C-4A13-9516-B7F9CED34113,426272AC-42C8-493C-9578-E99757097320,85B00519-246E-456B-A801-3394314B337A,F5CBA081-772B-4600-ACCA-AF7A28ED6A65,6887002F-E974-43D5-A084-F62770D2D3AE,8584F84E-7D47-4BCF-87CF-F271E56B8F9A,7C2D06CA-1DB3-4FF8-81D3-0A3AFAB4C2CF,2BB97E82-D9E6-4364-AD5D-F58A9E34AEAA,6973765E-22B0-4A07-B9EC-1CA8710ED8FD',',')
  相关解决方案