943,907 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 136575
  • MS SQL RSS
Oct 26th, 2005
0

insert multiple rows in database

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
sweetyp is offline Offline
4 posts
since Oct 2005
Oct 31st, 2005
0

Re: insert multiple rows in database

Quote 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


MS SQL Syntax (Toggle Plain Text)
  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
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Nov 17th, 2005
0

Re: insert multiple rows in database

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 ;-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rohitk2000 is offline Offline
1 posts
since Nov 2005
Nov 18th, 2005
0

Re: insert multiple rows in database

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
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Mar 14th, 2006
0

Re: insert multiple rows in database

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Texpert is offline Offline
14 posts
since Jan 2006
Jun 22nd, 2007
1

Re: insert multiple rows in database

Just replying for anyone looking for an answer. Using MS SQL, you can do the following:

MS SQL Syntax (Toggle Plain Text)
  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:

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JacobSteelsmith is offline Offline
1 posts
since Jun 2007
Feb 2nd, 2011
0
Re: insert multiple rows in database
hey can you help me for making a frame on sql
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dandoy is offline Offline
1 posts
since Feb 2011
Dec 1st, 2011
0

How to Insert multiple rows to table in SQL Server

It would be easier to use XML in SQL Server to insert multiple rows otherwise it becomes very tedious.

View full article with code explanations here [snipped]

Copy the following code into sql server to view a sample.

MS SQL Syntax (Toggle Plain Text)
  1. declare @test nvarchar(max)
  2.  
  3. SET @test = '<topic><dialog id="1" answerId="41">
  4. <comment>comment 1</comment>
  5. </dialog>
  6. <dialog id="2" answerId="42" >
  7. <comment>comment 2</comment>
  8. </dialog>
  9. <dialog id="3" answerId="43" >
  10. <comment>comment 3</comment>
  11. </dialog>
  12. </topic>'
  13.  
  14. declare @testxml xml
  15. SET @testxml = cast(@test AS xml)
  16. declare @answerTemp TABLE(dialogid int, answerid int, comment varchar(1000))
  17.  
  18. INSERT @answerTemp
  19. SELECT ParamValues.ID.value('@id','int') ,
  20. ParamValues.ID.value('@answerId','int') ,
  21. ParamValues.ID.value('(comment)[1]','VARCHAR(1000)')
  22. FROM @testxml.nodes('topic/dialog') AS ParamValues(ID)

View full article with code explanations here [snipped]
Last edited by Ezzaral; Dec 1st, 2011 at 6:02 pm. Reason: Snipped blog links. Please do not use the forums to promote your blog.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joe_bolla is offline Offline
1 posts
since Dec 2011
Message:
Previous Thread in MS SQL Forum Timeline: SQL Field creation Script for fields that may already exist
Next Thread in MS SQL Forum Timeline: crystal report connection information problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC