sql - insert data with comma delimiter but with different ID -


hi having problem bulk insertion.
challenge/objective save column comma delimiter table different id's
example->
selected query
@temp table

rolln                       fullname
441246                   john doe, jane more, john spade
441248                   jose mendez, ali muhm, jacob stile

to save in people table
result after insertion people->

people_id                    rollnumber               fullname
1                                   441246                      john doe
2                                   441246                      jane more
3                                   441246                      john spade

my script created repeating name

insert @temp_original select rollnumber, case when charindex(',',fullname)>0       substring(fullname,1,charindex(',',fullname)-1)       else fullname end fullname,  case when charindex(',',fullname)>0       replace(substring(fullname,charindex(',',fullname)+1,len(fullname)), ',', '')       else null end fullname2,  case when charindex(',',fullname)>0       replace(substring(fullname,charindex(',',fullname)+1,len(fullname)), ',', '')        else null end fullname3, [address], code, place, [country], legaltxt ( select newrec.rollnumber, newrec.fullname, newrec.[address], newrec.code, newrec.place, newrec.country, newrec.legaltxt  [dbo].[view_financecompare] newrec left join [dbo].[view_maincompare] oldrec on newrec.rollnumber = oldrec.rollnumber  oldrec.rollnumber null ) t    --print 'insert people' insert [dbo].[dbo_people] ([fullname], [createdon], [createdby]) select fullname, getdate(), rollnumber @temp_original union  select fullname2, getdate(), rollnumber @temp_original fullname2<>'' union  select fullname3, getdate(), rollnumber @temp_original fullname3<>'' 


helping hand appreciated.

you can recursive cte. here example:

create table t(id int, n nvarchar(max)) insert t values (1, 'a,b,c'), (2, 'e,f,g')  ; cte (   select    id,   n,   substring(n, 1, charindex(',', n)-1) name,   charindex(',',n) p   t    union    select id, n, substring(n, p+1, charindex(',', n, p)-1),   charindex(',', n, p+1)   cte   p <> 0 )  select id, namename cte order id, name 

fiddle: http://sqlfiddle.com/#!6/449bc/2


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -