| try custom split function |
| Senthil T. replied to Burak Gunay on Tuesday, December 19, 2006 2:53 PM |
|
refer the links
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=653&lngWId=5 |
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| November |
0 |
$0.00 |
0 |
| October |
0 |
$0.00 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
| Split function in your database |
| Peter Bromberg replied to Burak Gunay on Tuesday, December 19, 2006 4:21 PM |
|
will take the entire delimited string and return a table that you can program against:
CREATE FUNCTION [dbo].[fn_Split](@text varchar(8000), @delimiter varchar(20) = ' ') RETURNS @Strings TABLE ( position int IDENTITY PRIMARY KEY, value varchar(8000) ) AS BEGIN DECLARE @index int SET @index = -1 WHILE (LEN(@text) > 0) BEGIN SET @index = CHARINDEX(@delimiter , @text) IF (@index = 0) AND (LEN(@text) > 0) BEGIN INSERT INTO @Strings VALUES (@text) BREAK END IF (@index > 1) BEGIN INSERT INTO @Strings VALUES (LEFT(@text, @index - 1)) SET @text = RIGHT(@text, (LEN(@text) - @index)) END ELSE SET @text = RIGHT(@text, (LEN(@text) - @index)) END RETURN END
-- Example usage in a stored proc, to insert into Table variable: /* DECLARE @WORDS TABLE ( ID INT IDENTITY (1,1), string varchar(100) ) INSERT INTO @WORDS SELECT VALUE AS WORD FROM dbo.fn_Split( @WordsToInsert,' ') */
|
| Reply Reply Using Power Editor |
| Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Please post questions at forums, not via email! |  |
|
| |
Rank |
Winnings |
Points |
| November |
0 |
$0.00 |
0 |
| October |
0 |
$0.00 |
0 |
|
|
|
|
|
|
| thanks ,, and a quick question |
| Burak Gunay replied to Peter Bromberg on Wednesday, December 20, 2006 10:33 AM |
|
Peter,
I wrote a stored procedure using your function and it works fine. I was wondering how I could make the while statement at the end more elegant and add transaction functionality into it?
CREATE PROCEDURE dbo.wsp_Roles_AssignSchoolsByUserId @UserId uniqueidentifier, @Schools varchar(8000) AS
-- declare a table to hold school names DECLARE @USER_SCHOOLS TABLE ( ID INT IDENTITY (1,1), SCHOOL char(4) ) -- parse the delimited list of schools into individual schools -- and insert the school names into the user_schools table INSERT INTO @USER_SCHOOLS SELECT VALUE AS SCHOOL FROM dbo.fn_Split( @Schools,';')
-- check to see if table has any records DECLARE @COUNT int SELECT @COUNT = count(*) FROM @USER_SCHOOLS IF @COUNT > 0 BEGIN -- select individual school names and insert them along with the user id -- into tbl_UsersInSchools DECLARE @SCHOOL char(4) WHILE( @COUNT > 0 ) BEGIN SET @SCHOOL = (SELECT TOP 1 SCHOOL from @USER_SCHOOLS) INSERT INTO tbl_UsersInSchools VALUES (@UserId, @SCHOOL) DELETE FROM @USER_SCHOOLS WHERE SCHOOL = @SCHOOL SET @COUNT = @COUNT - 1 END END GO
|
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| November |
0 |
$0.00 |
0 |
| October |
0 |
$0.00 |
0 |
|
|
|
|
|
|
| alternative solution |
Saurabh Kaushik provided a rated reply to Burak Gunay on Friday, December 29, 2006 10:47 AM |
|
Here is some SQL code which might be useful for you, you can customize it as per your requirements. This does not use a table variable so should have a faster run time.
declare @school varchar(8000) declare @pos1 int declare @pos2 int declare @err_code INT
set @pos1 = 1 set @pos2 = 1
set @school = '1232;23;45;56;746;'
set @pos2 = charindex(';',@school)
while (@pos1<@pos2) begin begin transaction print substring(@school,@pos1,@pos2-@pos1) -- Insert statement if (@@Error>0) begin @err_code = @@Error end -- Update Statement if (@@Error>0) begin @err_code = @@Error end -- Delete statement if (@@Error>0) begin @err_code = @@Error end commit transaction
if(@err_code>0) begin PRINT 'Unexpected error occurred!' + STR(@err_code) Rollback transaction end set @pos1 = @pos2 + 1 set @pos2 = @pos2+charindex(';',substring(@school,@pos1,len(@school)- @pos1+1)) end
Hope this helps.
|
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| November |
0 |
$0.00 |
0 |
| October |
0 |
$0.00 |
0 |
|
|
|
|
|
|
|