| | |
insert multiple rows in database
![]() |
•
•
•
•
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?
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)
/******************************************************************************* 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
•
•
Join Date: Nov 2005
Posts: 1
Reputation:
Solved Threads: 0
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 ;-)
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 ;-)
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
"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
•
•
Join Date: Jun 2007
Posts: 1
Reputation:
Solved Threads: 0
Just replying for anyone looking for an answer. Using MS SQL, you can do the following:
This will insert 5 rows into the table. Testing this on SQL Server 2000, the following works:
MS SQL Syntax (Toggle Plain Text)
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:
MS SQL Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- updating multiple rows with one form (PHP)
- Insert Dataset into SQL Database. (VB.NET)
- How to store multiple selection? (VB.NET)
Other Threads in the MS SQL Forum
- Previous Thread: Seemingly Simple Issue
- Next Thread: Complex Query Help
Views: 78441 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business comparing comparison. connectingtodatabaseinuse count cursor data database dateadd datediff datepart dates day" dbsize deadlock delete_trigger exploit function getdate hack highperformancecomputing hpc hpcserver2008 ibm iis install joins limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security select server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables unicode-sql uniqueid update view weekday





