User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 397,658 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,375 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 44379 | Replies: 5
Reply
Join Date: Oct 2005
Posts: 4
Reputation: sweetyp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sweetyp sweetyp is offline Offline
Newbie Poster

insert multiple rows in database

  #1  
Oct 26th, 2005
How do i insert multiple rows in database.
for example: I have 3 list:
list1: a1,a2,a3,a4
list2: b1,b2,b3,b4
list3: c1,c2,c3,c4

I want to insert a1,b1,c1 in 1st row. a2,b2,c2 in 2nd row,...
How do I write one sql insert statement to do that?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 481
Reputation: pty is on a distinguished road 
Rep Power: 3
Solved Threads: 31
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: insert multiple rows in database

  #2  
Oct 31st, 2005
Originally Posted by sweetyp
How do i insert multiple rows in database.
for example: I have 3 list:
list1: a1,a2,a3,a4
list2: b1,b2,b3,b4
list3: c1,c2,c3,c4

I want to insert a1,b1,c1 in 1st row. a2,b2,c2 in 2nd row,...
How do I write one sql insert statement to do that?

i'd imagine making a stored procedure to deal with the inserts would be the easiest way.

you can modify this sp to scroll through the list and put the whole lot of your list into variables then just insert 'em.

hope this helps


/*******************************************************************************
Description             Delimit a string and return specified segment
Author:                     Peter Yates
Example usage:        exec delimiter 'te~st~in~g1~23', '~', 5
Modifications:
*******************************************************************************/

if exists  (select * 
            from dbo.sysobjects 
            where id = object_id(N'[dbo].[delimiter]') 
            and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[delimiter]
go

create procedure delimiter(
 @str nvarchar (4000), --delimited string
 @del nvarchar (10), --delimiter
 @sect int --section of string wanted
 )
as
begin
 declare @nextstr nvarchar(4000)
 declare @pos int
 declare @nextpos int
 create table #valuetable (id int identity, value varchar(500))
 set @nextstr = ''
 set @str = @str + @del
 set @pos = charindex(@del,@str)
 set @nextpos = 1
 while (@pos <>  0)  
 begin
  set @nextstr = substring(@str,1,@pos - 1)
     insert into #valuetable 
      ([value]) 
      values 
      (@nextstr)
  set @str = substring(@str,@pos +1,len(@str))
  set @nextpos = @pos
  set @pos  = charindex(@del,@str)
 end
   select value 
   from #valuetable
   where id = @sect
 return
end
Reply With Quote  
Join Date: Nov 2005
Posts: 1
Reputation: rohitk2000 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rohitk2000 rohitk2000 is offline Offline
Newbie Poster

Re: insert multiple rows in database

  #3  
Nov 17th, 2005
Why use a stored procedure when we can do it in one sql??

This is an example of how to to it ....

INSERT INTO SEND_RULE(MRT_CODE, ISSUING_COMPANY_CODE, CARD_BRAND_CODE, SEND_ISSUING_COMPANY_CODE)
(SELECT MRT_CODE, ISSUING_COMPANY_CODE, CARD_BRAND_CODE, ISSUING_COMPANY_CODE SEND_ISSUING_COMPANY_CODE
FROM CARD_MRT_CONTRACT)
ORDER BY MRT_CODE, ISSUING_COMPANY_CODE, CARD_BRAND_CODE;

:mrgreen:

Rohit Kumar Singh

P.S> Im the best ;-)
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 481
Reputation: pty is on a distinguished road 
Rep Power: 3
Solved Threads: 31
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: insert multiple rows in database

  #4  
Nov 18th, 2005
that would work if

a) the values were in another table

b) and they werent in a comma delimited list as per the original question
Reply With Quote  
Join Date: Jan 2006
Posts: 14
Reputation: Texpert is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Texpert's Avatar
Texpert Texpert is offline Offline
Newbie Poster

Re: insert multiple rows in database

  #5  
Mar 14th, 2006
I tried to use your solution but while creating sp for inserting rows, I ran into a problem, My table has more columns so when I say
"Insert into table1 (col1,col2,col3) values (@Nextstr)"
the sp compiler gives me error that column list has more items than value list. So just to trick that I created a columnstr and passed @Columnstr into Insert statement so now it looks like
"Insert into table1 (@Columnstr) values (@Nextstr)"
now it compiles ok, but runtime it gives error 'table1 object not found'

any idea ? any help is deeply appreciated.

thanks in advance.
TJ
Reply With Quote  
Join Date: Jun 2007
Posts: 1
Reputation: JacobSteelsmith is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
JacobSteelsmith JacobSteelsmith is offline Offline
Newbie Poster

Re: insert multiple rows in database

  #6  
Jun 22nd, 2007
Just replying for anyone looking for an answer. Using MS SQL, you can do the following:

INSERT INTO test_table
SELECT 62
UNION
SELECT 91
UNION
SELECT 95
UNION 
SELECT 98
UNION 
SELECT 99

This will insert 5 rows into the table. Testing this on SQL Server 2000, the following works:

CREATE TABLE test
(
	val1 VARCHAR(10), 
	val2 VARCHAR(10), 
	val3 VARCHAR(10), 
	val4 VARCHAR(10)
)
GO

INSERT INTO test
(val1, val2, val3, val4)
(SELECT 'a1', 'a2', 'a3', 'a4')
UNION
(SELECT 'b1', 'b2', 'b3', 'b4')
UNION
(SELECT 'c1', 'c2', 'c3', 'c4')
GO
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 11:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC