﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Insert values from array into a table / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 18:02:23 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>[quote][b]Jaat (5/2/2012)[/b][hr]I´m sorry i did not know the rules to answer something, but based on best practices we should avoid making string operations on the database side for performance that´s my understanding, beside i add some code IF OBJECT_ID('#Tbl_TempTable') IS NULLBEGIN	CREATE TABLE #Tbl_TempTable	(		ID INT IDENTITY	   ,FName VARCHAR(30)	   ,EnterDtm DATETIME DEFAULT GETDATE()	)ENDDECLARE @XML XMLSET @XML = '&amp;lt;Root&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Valdez Calambuco&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Primero&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Segundo&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Tercero&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Cuarto&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Quinto&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Sexto&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Septimo&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Octavo&amp;lt;/PrimerNombre&amp;gt;&amp;lt;/Root&amp;gt;'INSERT INTO #Tbl_TempTable (FName)SELECT T.c.value('.','VARCHAR(30)') from @XML.nodes('//Root/PrimerNombre') T(c)SELECT * FROM #Tbl_TempTablelet me know if it works, if not i will try to provide a better solutions thanks[/quote]That would be helpful but unfortunately it is not what the OP is trying to solve. They have a comma separated list and want to parse that into individual rows.Not sure if you noticed but this thread is 3 years old and the OP has not logged in for about a 1 1/2 years. :-)</description><pubDate>Wed, 02 May 2012 13:06:19 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>I´m sorry i did not know the rules to answer something, but based on best practices we should avoid making string operations on the database side for performance that´s my understanding, beside i add some code IF OBJECT_ID('#Tbl_TempTable') IS NULLBEGIN	CREATE TABLE #Tbl_TempTable	(		ID INT IDENTITY	   ,FName VARCHAR(30)	   ,EnterDtm DATETIME DEFAULT GETDATE()	)ENDDECLARE @XML XMLSET @XML = '&amp;lt;Root&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Valdez Calambuco&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Primero&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Segundo&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Tercero&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Cuarto&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Quinto&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Sexto&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Septimo&amp;lt;/PrimerNombre&amp;gt;	&amp;lt;PrimerNombre&amp;gt;Juan Octavo&amp;lt;/PrimerNombre&amp;gt;&amp;lt;/Root&amp;gt;'INSERT INTO #Tbl_TempTable (FName)SELECT T.c.value('.','VARCHAR(30)') from @XML.nodes('//Root/PrimerNombre') T(c)SELECT * FROM #Tbl_TempTablelet me know if it works, if not i will try to provide a better solutions thanks</description><pubDate>Wed, 02 May 2012 12:51:26 GMT</pubDate><dc:creator>Jaat</dc:creator></item><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>[quote][b]Jaat (5/1/2012)[/b][hr]It´s better if u use an XML solution.[/quote]Great.  Let's see the code!</description><pubDate>Tue, 01 May 2012 17:34:02 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>It´s better if u use an XML solution.</description><pubDate>Tue, 01 May 2012 16:29:28 GMT</pubDate><dc:creator>Jaat</dc:creator></item><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>Neat solution! Thanks :)</description><pubDate>Tue, 28 Jul 2009 08:29:44 GMT</pubDate><dc:creator>Allister Reid</dc:creator></item><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>Another tally table solution, but this one doesn't require the creation of a separate user defined function.   CTEs are used to parse the "ARRAY" into a table which can be joined to "MyOtherTable" to support your insert. [code]DECLARE @input VARCHAR(2002)DECLARE @array VARCHAR(2000)DECLARE @sepchar CHAR(1)set @sepchar = ','  -- separation character is a commaset @array= '321,3AY,3LH,3PU,3PR,3SP,3JH,3VN,3VR,3VT,3VU,3VV,3VW,3VY,3UW,3WA,3WD,3WI,3WM,3WY,3WZ,3XB,3XJ,3XK'set @input = @sepchar + @array + @sepchar  -- begin and end with separation characters;WITH tally (N) as (SELECT TOP 1000000 row_number() OVER (ORDER BY sc1.id) FROM Master.dbo.SysColumns sc1 CROSS JOIN Master.dbo.SysColumns sc2),ArrayAsTable AS(SELECT substring(@input,N+1,CHARindex(@sepchar,@input,N+1)-(N+1)) as element FROM tally WHERE substring(@input,N,1) = @sepchar  and N &lt; len(@input))-- INSERT INTO MyTable (col1,col2,col3)SELECT Element FROM ArrayAsTable-- JOIN MyOtherTable ON (whatever)[/code]</description><pubDate>Tue, 28 Jul 2009 08:20:53 GMT</pubDate><dc:creator>The Dixie Flatline</dc:creator></item><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>Hi,what about a set based solution?  It'll run faster and scale better.  This code courtesy of Jeff [url]http://www.sqlservercentral.com/articles/T-SQL/62867/[/url]We'll need a tally table for this so, set one up:[code]    USE TempDB     --DB that everyone has where we can cause no harm    SET NOCOUNT ON --Supress the auto-display of rowcounts for appearance/speed--=============================================================================--      Create and populate a Tally table--=============================================================================--===== Conditionally drop      IF OBJECT_ID('dbo.Tally') IS NOT NULL         DROP TABLE dbo.Tally--===== Create and populate the Tally table on the fly SELECT TOP 11000 --equates to more than 30 years of dates        IDENTITY(INT,1,1) AS N   INTO dbo.Tally   FROM Master.dbo.SysColumns sc1,        Master.dbo.SysColumns sc2--===== Add a Primary Key to maximize performance  ALTER TABLE dbo.Tally    ADD CONSTRAINT PK_Tally_N         PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100--===== Let the public use it  GRANT SELECT, REFERENCES ON dbo.Tally TO PUBLIC[/code]Re-writing split function using tally table, again, this was lifted almost as is from article referenced above:[code]create function [dbo].[fn_split](	@str varchar(8000),	@spliter char(1)	)	returns @returnTable table (idx int primary key identity, item varchar(8000))	as	begin	declare @spliterIndex int	select @str = @str + @spliter	SELECT @str = @spliter + @str + @spliter		INSERT @returnTable	SELECT SUBSTRING(@str,N+1,CHARINDEX(@spliter,@str,N+1)-N-1)   FROM dbo.Tally  WHERE N &lt; LEN(@str)    AND SUBSTRING(@str,N,1) = @spliter  ORDER BY N	  	returnend[/code]Check it all works:[code]	SELECT * FROM fn_split('321,3AY,3LH,3PU,3PR,3SP,3JH,3VN,3VR,3VT,3VU,3VV,3VW,3VY,3UW,3WA,3WD,3WI,3WM,3WY,3WZ,3XB,3XJ,3XK')[/code]@KB,In fact, for true scalability (how long/how many elements does comma separated list have?) I wouldn't bother with the table value function, I'd just do the split in insert into mytable statement, however, I don't know how you are doing the select/join with myothertable so I can't provide code for this!Please provide table ddls and data as per [url]http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url] and someone'll be able to provide fully tested code.</description><pubDate>Tue, 28 Jul 2009 08:12:58 GMT</pubDate><dc:creator>Allister Reid</dc:creator></item><item><title>RE: Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>i think you want that all , seperated values must be returned in table format.use following function to do this.select * from fn_split('321,3AY,3LH,3PU,3PR,3SP,3JH,3VN,3VR,3VT,3VU,3VV,3VW,3VY,3UW,3WA,3WD,3WI,3WM,3WY,3WZ,3XB,3XJ,3XK')create function [dbo].[fn_split](@str varchar(8000),@spliter char(1))returns @returnTable table (idx int primary key identity, item varchar(8000))asbegindeclare @spliterIndex intselect @str = @str + @spliterwhile len(@str) &gt; 0	begin		select @spliterIndex = charindex(@spliter,@str)		if @spliterIndex = 1			insert @returnTable (item)				values (null)		else			insert @returnTable (item)				values (substring(@str, 1, @spliterIndex-1))		select @str = substring(@str, @spliterIndex+1, len(@str)-@spliterIndex)	endreturnend</description><pubDate>Tue, 28 Jul 2009 05:44:15 GMT</pubDate><dc:creator>shamassaeedmr</dc:creator></item><item><title>Insert values from array into a table</title><link>http://www.sqlservercentral.com/Forums/Topic760680-338-1.aspx</link><description>Hi,Is there a way in SQL Server 2005 to achieve this?[code]declare @array varchar(2000)set @array='321,3AY,3LH,3PU,3PR,3SP,3JH,3VN,3VR,3VT,3VU,3VV,3VW,3VY,3UW,3WA,3WD,3WI,3WM,3WY,3WZ,3XB,3XJ,3XK'--Loop until all the values in @arrayinsert into myTable(Col1,Col2,Col3)select @array --only 3digits must be selected,col2,col3 from myOtherTable[/code]Thanks,KB</description><pubDate>Tue, 28 Jul 2009 05:40:25 GMT</pubDate><dc:creator>KBSK</dc:creator></item></channel></rss>