| Author |
Topic |
|
shaoen01
Yak Posting Veteran
78 Posts |
Posted - 2006-01-15 : 02:04:07
|
I have been reading the user defined function, Split, but do not really understand the split method. Can anyone tell me what exactly will "Data" in the split function return?Will it return a insert statement? ThanksCREATE FUNCTION SPLIT(@RowData nvarchar(2000),@SplitOn nvarchar(5))RETURNS @RtnValue table(Id int identity(1,1),Data nvarchar(100))ASBEGINDeclare @Cnt intSet @Cnt = 1While (Charindex(@SplitOn,@RowData)>0)BeginInsert Into @RtnValue (data)SelectData = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))Set @Cnt = @Cnt + 1EndInsert Into @RtnValue (data)Select Data = ltrim(rtrim(@RowData))ReturnEND |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-15 : 02:30:23
|
Execute it like this. 2 columns will be returned. Column Id is the sequence of data in the @RowData. Column Data will be the data itself.select *from dbo.SPLIT('1,4,3,5, 35 , 34', ',')Result :Id Data ----------- ----1 12 43 34 55 356 34(6 row(s) affected)-----------------'KH' |
 |
|
|
shaoen01
Yak Posting Veteran
78 Posts |
Posted - 2006-01-15 : 02:37:46
|
If my stored procedure is as shown below, what will the data be?CREATE PROCEDURE [ADDITEMPROM] (@PROMOTIONID INT,@ITEMIDS VARCHAR(500))AsINSERT INTO ITEMPROM(PROMOTIONID, ITEMID)SELECT @PROMOTIONID, [Data]FROM DBO.SPLIT(@ITEMIDS, ',')GO |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-15 : 02:49:14
|
| Data will be individual item specified in @ITEMIDSfor example if @ITEMIDS is 'abc,def,ghi', the SPLIT function will split the string 'abc',def',ghi' into 3 individual string 'abc', 'def', 'ghi'-----------------'KH' |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-15 : 02:51:56
|
| Try this . . .exec ADDITEMPROM 100, 'abc,def,ghi,jkl'select * from ITEMPROM-----------------'KH' |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-01-15 : 03:21:02
|
| http://sqlteam.com/forums/topic.asp?TOPIC_ID=50648 |
 |
|
|
|
|
|