Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Inserts with MS SQL Server

Status
Not open for further replies.

esdee

Programmer
Apr 12, 2001
143
US
in MySQL there is this syntax for using a single insert for multiple records:
Code:
INSERT INTO x (a,b)
VALUES 
 ('1', 'one'),
 ('2', 'two'),
 ('3', 'three')
is there similar syntax in MS SQL Server 2000 ?
i came up with
Code:
insert  (a, b)
select '1', 'one' 
union 
select '2', 'two'
union 
select '3', 'three'

but im not sure about its performance.

another approach i discovered is in : passing a string with delimited values, parsing and inserting, but wanted to see other ways.

my case is an Application in c#, passing the values in an xml to the DB, but with large xmls it ... chokes...

------------------------
 
You can pass XML and then do a straight insert from it.

e.g.
Code:
declare @pi_XMLUpdateString varchar(1000)
SET @pi_XMLUpdateString = '<root>
		<node1>
			<subnode a="1" b="one" />
			<subnode a="2" b="two" />
			<subnode a="3" b="three" />
		</node1>
	</root>'
--Execute the inbuilt XML stored proc handler
EXEC dbo.sp_xml_preparedocument @FileHandle output, @pi_XMLUpdateString 
INSERT INTO tableX (a, b)
SELECT 
	*
FROM
	OPENXML(@FileHandle, 'root/node1/subnode', 1 ) 
	WITH (	accountOID       Int         	'@a',
		status 		Int 		'@b')

EXEC dbo.sp_xml_removedocument @FileHandle

"I'm living so far beyond my income that we may almost be said to be living apart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top