Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SP to insert a list

Posted on 1998-12-07
2
Medium Priority
?
194 Views
Last Modified: 2010-03-19
I am attempting to write a stored procedure to insert multiple records based on a list stored in a string (varchar). The first parameter is the an ID number and the second parameter is the list (ie "1,2,4,6").  I would like suggestion on either parsing the list then running many insert of the type INSERT table (ID,Value) VALUES (@Id,@Value) or can I do a batch insert with nested selects.
0
Comment
Question by:amnh
2 Comments
 
LVL 3

Accepted Solution

by:
hakyemez earned 600 total points
ID: 1092120
/*@yourstring = '1,2,4,6'*/
declare @id int
declare @value int
declare @pos int,@newstr varchar(255),@bExit int
select @pos=CharIndex(",",@yourstring)
select @id=Convert(int,SubString(@yourstring,1,@pos-1))
select @newstr=SubString(@yourstring,@pos+1,datalength(@yourstring)-@pos)
select @bExit=-1
while bExit=-1
begin
    select @pos=Charindex(",",@newstr)
    if @pos<>0
    begin
        select @value = Convert(int,SubString(@newstr,1,@pos-1))
        insert into table (Id,Value) values (@id,@value)
        select @newstr = SubString(@newstr,@pos+1,datalength(@newstr)-@pos)
    end
    else select bExit=0
end



0
 

Author Comment

by:amnh
ID: 1092121
Good Answer.
I changed slightly by the passing the @id as seperate parameter.
@ID integer,
@yourstring varchar(255)
as
declare @value int
declare @pos int,@newstr varchar(255),@bExit int
select @pos=CharIndex(',',@yourstring)
select @newstr=@yourstring
select @bExit=-1
while @bExit=-1
begin
      select @pos=Charindex(',',@newstr)
      if @pos<>0
      begin
            select @value = Convert(int,SubString(@newstr,1,@pos-1))
            insert into tblUserListMem (User_ID,UserList_ID) values (@id,@value)
            select @newstr = SubString(@newstr,@pos+1,datalength(@newstr)-@pos)
      end
      else
      begin
            select @bExit=0
            select @value = Convert(int,@newstr)
            insert into table (User_ID,UserList_ID) values (@id,@value)
      end
end
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question