Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch insert/update using stored procedure

Tags:

sql

sql-server

Can anyone give me a sample script for batch insert/update of records in table using stored procedure in SQL Server?

like image 432
user335160 Avatar asked Oct 13 '22 23:10

user335160


1 Answers

I've done something like this in the past:

CREATE PROCEDURE InsertProductIds(@ProductIds xml) AS

INSERT INTO Product (ID) 
SELECT ParamValues.ID.value('.', 'VARCHAR(20)')
FROM   @ProductIds.nodes('/Products/id') as ParamValues(ID) 

END

Obviously this is just a single-column table, but the XML approach applies for multi-column tables as well. You then do this:

EXEC InsertProductIds @ProductIds='<Products><id>3</id><id>6</id></Products>'
like image 99
HTTP 410 Avatar answered Oct 18 '22 03:10

HTTP 410