insert multiple rows in database

Reply

Join Date: Oct 2005
Posts: 4
Reputation: sweetyp is an unknown quantity at this point 
Solved Threads: 0
sweetyp sweetyp is offline Offline
Newbie Poster

insert multiple rows in database

 
1
  #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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 524
Reputation: pty is on a distinguished road 
Solved Threads: 37
pty's Avatar
pty pty is offline Offline
Posting Pro

Re: insert multiple rows in database

 
0
  #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


  1. /*******************************************************************************
  2. Description Delimit a string and return specified segment
  3. Author: Peter Yates
  4. Example usage: exec delimiter 'te~st~in~g1~23', '~', 5
  5. Modifications:
  6. *******************************************************************************/
  7.  
  8. IF EXISTS (SELECT *
  9. FROM dbo.sysobjects
  10. WHERE id = object_id(N'[dbo].[delimiter]')
  11. AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
  12. DROP procedure [dbo].[delimiter]
  13. go
  14.  
  15. CREATE procedure delimiter(
  16. @str nvarchar (4000), --delimited string
  17. @del nvarchar (10), --delimiter
  18. @sect int --section of string wanted
  19. )
  20. AS
  21. begin
  22. declare @nextstr nvarchar(4000)
  23. declare @pos int
  24. declare @nextpos int
  25. CREATE TABLE #valuetable (id int identity, value varchar(500))
  26. SET @nextstr = ''
  27. SET @str = @str + @del
  28. SET @pos = charindex(@del,@str)
  29. SET @nextpos = 1
  30. while (@pos <> 0)
  31. begin
  32. SET @nextstr = substring(@str,1,@pos - 1)
  33. INSERT INTO #valuetable
  34. ([value])
  35. VALUES
  36. (@nextstr)
  37. SET @str = substring(@str,@pos +1,len(@str))
  38. SET @nextpos = @pos
  39. SET @pos = charindex(@del,@str)
  40. end
  41. SELECT value
  42. FROM #valuetable
  43. WHERE id = @sect
  44. RETURN
  45. end
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 1
Reputation: rohitk2000 is an unknown quantity at this point 
Solved Threads: 0
rohitk2000 rohitk2000 is offline Offline
Newbie Poster

Re: insert multiple rows in database

 
0
  #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 Quick reply to this message  
Join Date: Oct 2005
Posts: 524
Reputation: pty is on a distinguished road 
Solved Threads: 37
pty's Avatar
pty pty is offline Offline
Posting Pro

Re: insert multiple rows in database

 
0
  #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 Quick reply to this message  
Join Date: Jan 2006
Posts: 14
Reputation: Texpert is an unknown quantity at this point 
Solved Threads: 0
Texpert's Avatar
Texpert Texpert is offline Offline
Newbie Poster

Re: insert multiple rows in database

 
0
  #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 Quick reply to this message  
Join Date: Jun 2007
Posts: 1
Reputation: JacobSteelsmith is an unknown quantity at this point 
Solved Threads: 0
JacobSteelsmith JacobSteelsmith is offline Offline
Newbie Poster

Re: insert multiple rows in database

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

  1. INSERT INTO test_table
  2. SELECT 62
  3. UNION
  4. SELECT 91
  5. UNION
  6. SELECT 95
  7. UNION
  8. SELECT 98
  9. UNION
  10. SELECT 99

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

  1. CREATE TABLE test
  2. (
  3. val1 VARCHAR(10),
  4. val2 VARCHAR(10),
  5. val3 VARCHAR(10),
  6. val4 VARCHAR(10)
  7. )
  8. GO
  9.  
  10. INSERT INTO test
  11. (val1, val2, val3, val4)
  12. (SELECT 'a1', 'a2', 'a3', 'a4')
  13. UNION
  14. (SELECT 'b1', 'b2', 'b3', 'b4')
  15. UNION
  16. (SELECT 'c1', 'c2', 'c3', 'c4')
  17. GO
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum


Views: 78441 | Replies: 5
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC