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
 General SQL Server Forums
 New to SQL Server Programming
 sp - insert into table

Author  Topic 

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-05-07 : 07:32:00
destination Table schema
Plan_id int
location_id int
user_id int

my data is coming as below
'72|12,35,68,17,16$40|12,43,38',1
where 72 is the plan id and next are the location_id and new row data is splited with &
last input is created by .

need to create sp for insert into table

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-07 : 07:38:46
your explanation doesnt make sense. & doesnt even appear in your sample data

which all of above values will fall into location field and which all into user_id field?
also I assume each rows needs to be split up to multiple rows in table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-05-07 : 08:26:21
looking for the below output ::

Plan Id--- location ID--- UserID

72 ---12---1

72 ---35---1

72 ---68---1

72 ---17---1

72 ---16---1

40 ---12---1

40 ---43---1

40 ---38---1

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-05-07 : 09:10:06
Declare @s Varchar(100) = '72|12,35,68,17,16$40|12,43,38'
Select * into #Temp From dbo.Split('72|12,35,68,17,16$40|12,43,38','$')
Select SUBSTRING(items,0,CHARINDEX('|',items,0)) Item,SUBSTRING(items,CHARINDEX('|',items,0)+1,LEN(items)) Value
into #temp1 From #Temp A
Select A.Item 'PlanID',B.items 'LocationID',1 'UserID' From #temp1 A
Cross Apply(Select * From dbo.Split(A.Value,','))B
Drop table #Temp,#temp1


THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-08 : 00:26:54
you need to use string parsing alogirthm like below and pass the corresponding delimiter to get data parsed out as rows

see

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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -