Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Insert comma seperated values into multiple column

Author  Topic 

pradeepmanne
Starting Member

31 Posts

Posted - 2012-01-09 : 06:06:05
hi,
i want to insert comma separated values into datatable table
for example
the comma saperated string like '123,char,456,varh'
has to be inserted into table like using a storeprocedure
col1 col2,col3 col4
123 char 456 varh

Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-09 : 06:12:33
you need a string parsing function to parse the values and then put it into table
but few questions before that
will there be always 4 values? will they always go into the same table fields?
in case there are multiple sets, how do you distingush end of a set of values?

below you can find a typical string parsing function

http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pradeepmanne
Starting Member

31 Posts

Posted - 2012-01-09 : 06:15:52
hi,
thanks for the reply
the values will be changing but the tablename will be same
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-09 : 06:18:35
do you mean number of values will change?
then how will you determine which values should go to which field

it would be much better if you can explain scenario with sample data and final output

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pradeepmanne
Starting Member

31 Posts

Posted - 2012-01-09 : 06:21:59
hi ,
the sequence of data for columns am using a arraylist in my front end
that list values i have converted into a comma saperate string
list[0]=123
list[1]=char
.
.
this is the commsaperate value '123,char,456,varh'
col1 will be '123'
col2 will be 'char' so on ...
Go to Top of Page

NeilG
Aged Yak Warrior

530 Posts

Posted - 2012-01-09 : 06:44:56
look on BOL for BULK INSERT Command that may help you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-09 : 06:46:54
quote:
Originally posted by pradeepmanne

hi ,
the sequence of data for columns am using a arraylist in my front end
that list values i have converted into a comma saperate string
list[0]=123
list[1]=char
.
.
this is the commsaperate value '123,char,456,varh'
col1 will be '123'
col2 will be 'char' so on ...



so is it always a single record values you pass?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pradeepmanne
Starting Member

31 Posts

Posted - 2012-01-09 : 07:09:32
Yes,each time a single record into a table

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-01-09 : 08:07:35
See if this helps
http://beyondrelational.com/blogs/madhivanan/archive/2008/09/11/splitting-delimited-data-to-columns-set-based-approach.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-09 : 09:45:00
quote:
Originally posted by pradeepmanne

Yes,each time a single record into a table





then just use a parsing function like one posted in first link

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -