October 21, 2013 at 7:30 pm
Hi,
I have a stored proc which has two input parameters.
@id bigint, @string nvarchar(max)
@string is comma delimited ex : "100,200,300,400,500".
I wanted to split this comma delimited string and insert into table "Sample" with same id.
sample input my proc :
1, "100,200,300,400,500"
desired output :
Table Name : sample:
1 100
1 200
1 300
1 400
1 500
Jeff has give nice way to split the string with the delimiter.
select * from dbo.DelimitedSplit8K('100,200,300,400,500', ',')
Is there any way to avoid looping(while loop) and insert into "sample" table
or any suggestion how to proceed this more faster way
October 21, 2013 at 9:07 pm
Like this?
INSERT INTO SampleTable
SELECT 1, Item
FROM dbo.DelimitedSplit8K('100,200,300,400,500', ',')
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 21, 2013 at 9:50 pm
Hi,
Try these
Declare @id bigint = 1
Declare @string as varchar(100) ='100,200,300,400,500'
DECLARE @testXML XML
SET @testXML = N'<test>' + REPLACE(@string,',','</test><test>') + '</test>'
SELECT @id [id],node.value('.','varchar(100)') as [No]
FROM @testXML.nodes('/test') as records(node)
October 21, 2013 at 10:03 pm
hemang.patel (10/21/2013)
Hi,Try these
Declare @id bigint = 1
Declare @string as varchar(100) ='100,200,300,400,500'
DECLARE @testXML XML
SET @testXML = N'<test>' + REPLACE(@string,',','</test><test>') + '</test>'
SELECT @id [id],node.value('.','varchar(100)') as [No]
FROM @testXML.nodes('/test') as records(node)
How does that insert into a sample table like the OP asked?
The discussion thread of the article that presents DelimitedSplit8K[/url] covers this approach to string splitting. It is recommended reading.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 22, 2013 at 3:40 am
October 22, 2013 at 3:53 am
Hi,
Use the following Function....
CREATE FUNCTION fn_getrowsfrmCSVstring(@str varchar(max))
returns @rtn table (id int identity(1,1),value varchar(2000))
As
Begin
Declare
@mstr varchar(max),@i INT,@len INT
SELECT @mstr=@str,@len=LEN(@str),@i=1
WHILE @i<@len
BEGIN
IF CHARINDEX(',',@str)>0
SELECT @STR=SUBSTRING(@str,1,CHARINDEX(',',@str)-1)
ELSE
Select @STR=SUBSTRING(@str,1,LEN(@str))
INSERT INTO @rtn SELECT @STR
SET @i=@i+LEN(@str)+1
SET @STR=SUBSTRING(@mstr,@i,@len)
END
Return
END
October 22, 2013 at 4:02 am
Pulivarthi Sasidhar (10/22/2013)
Hi,Use the following Function....
CREATE FUNCTION fn_getrowsfrmCSVstring(@str varchar(max))
returns @rtn table (id int identity(1,1),value varchar(2000))
As
Begin
Declare
@mstr varchar(max),@i INT,@len INT
SELECT @mstr=@str,@len=LEN(@str),@i=1
WHILE @i<@len
BEGIN
IF CHARINDEX(',',@str)>0
SELECT @STR=SUBSTRING(@str,1,CHARINDEX(',',@str)-1)
ELSE
Select @STR=SUBSTRING(@str,1,LEN(@str))
INSERT INTO @rtn SELECT @STR
SET @i=@i+LEN(@str)+1
SET @STR=SUBSTRING(@mstr,@i,@len)
END
Return
END
Just guessing mind you but I believe the DelimitedSplit8K FUNCTION will be a tad faster than this approach. Suggest you read the linked article as well.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 22, 2013 at 4:51 am
declare @input varchar(50)='100,200,300,400,500'
select '1', q2.value from
( SELECT cast('<x>'+replace(@input,',','</x><x>')+'</x>' as xml) as txml ) q1 CROSS APPLY
( SELECT x.value('.','varchar(100)') as value FROM txml.nodes('x') as f(x) )q2
October 22, 2013 at 5:20 am
wow,
thanks a lot everyone for your time on this post.
dwain's suggestion will be more simple.
October 22, 2013 at 5:48 am
born2achieve (10/22/2013)
wow,thanks a lot everyone for your time on this post.
dwain's suggestion will be more simple.
It'll be significantly faster, too. If you haven't read the article on the delimited string splitter, I'd highly suggest you do so. This is a serious function to have in your toolbox.
October 22, 2013 at 7:28 am
SrcName (10/22/2013)
declare @input varchar(50)='100,200,300,400,500'select '1', q2.value from
( SELECT cast('<x>'+replace(@input,',','</x><x>')+'</x>' as xml) as txml ) q1 CROSS APPLY
( SELECT x.value('.','varchar(100)') as value FROM txml.nodes('x') as f(x) )q2
You too should have a read of that article. In case you missed it, you can find it in my signature too. The article about splitting strings. It is a serious mind opening read.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 22, 2013 at 7:32 am
Hello Ed Wagner,
I totally agree that "DelimitedSplit8K" function is much faster. thanks for your time.
October 22, 2013 at 8:40 am
born2achieve (10/22/2013)
Hello Ed Wagner,I totally agree that "DelimitedSplit8K" function is much faster. thanks for your time.
No problem - glad I could help. I cannot, however, take any credit for coming up with DelimitedSplit8K. That credit goes to Jeff Moden.
October 22, 2013 at 12:25 pm
Yes i agree that credit goes to Jeff.
by the way, i am good with insert statement. Any suggestion with update statement? how can i update the sample table with the out come of funtion returns table? any suggestions please
October 23, 2013 at 5:09 am
can any one please suggest me
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply