c# - Insert list of values into a SQL Server table with stored procedure -


How can I get a list of column data in a stored procedure?

My stored procedure is

  optional functions [dbo]. Start in the form of dbo.Group (id, name) values ​​(@ ID, @ name) SELECT 0 END START CATCH SELECT 1 END CATCH END GO  @ id int, @ Name as Navkararak (50) Code>  

I want to pass in the table like this data to be inserted

  @ id = 1, 2,3,4,5 @ name = 'Test1, test2, test3, test4, test5'  

and as a result

  ID name 1 test1 2 "test2" 3 test3 4 test4 5 test5  

A "List" or "Array" in SQL Server ..... Table Therefore, if you are on SQL Server 2008 or new (you have not specified), then the table's table by using the table-value parameters feature of SQL Server In the stored procedure

  - To match your input parameters, create a table type TYPE IdNameTable as table (ID INT, named NVARCHAR (50)); Go - Change your stored procedure to accept such a table type parameter. Alternative process [dbo]. [Register] Start as VALUE IdNameTable initially in dbo.Group (ID, name) - Get value from table type parameter SELECT Id, by name name = Values ​​SELECT 0 END START CATCH SELECT-1 END CATCH END GO  

See the Available broadly and freely for more information Table-Value Parameter Feature and How to Use it

If you want to use it with T-SQL, use this code:

  - Announce a variable in that table type DECLARE @InputTable IdNameTable - Insert ININSERT @InputTable (ID, Name) values ​​in that table variable (1, 'Test 1'), (2, 'Test 2') - this table Execute your stored procedure with the input parameter EXECUTE [dbo]. [Register] @InputTable  

If you want to use it from C # or VB.NET, see Michael Edenfield's links in comments.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -