Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing DataTable to stored procedure as an argument

I have a data table created in C#.

DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));

dt.Rows.Add("James", 23);
dt.Rows.Add("Smith", 40);
dt.Rows.Add("Paul", 20);

I want to pass this to the following stored procedure.

CREATE PROCEDURE  SomeName(@data DATATABLE)
AS
BEGIN
    INSERT INTO SOMETABLE(Column2,Column3)
    VALUES(......);
END

My question is : How do we insert those 3 tuples to the SQL table ? do we need to access the column values with the dot operator ? or is there any other way of doing this?

like image 521
nidarshani fernando Avatar asked Apr 08 '15 06:04

nidarshani fernando


People also ask

Can we pass table as a parameter to stored procedure?

Table-Valued Parameters aka TVPs are commonly used to pass a table as a parameter into stored procedures or functions. They are helpful in a way, we can use a table as an input to these routines and we can get rid of dealing more complex steps to achieve this process.

Can we pass table as parameter in stored procedure in mysql?

Passing table-valued parameters to a stored procedure is a three-step process: Create a user-defined table type that corresponds to the table that you want to populate. Pass the user-defined table to the stored procedure as a parameter.

How do you execute a stored procedure with table valued parameters in SQL?

First a Table Variable of User Defined Table Type has to be created of the same schema as that of the Table Valued parameter. Then it is passed as Parameter to the Stored Procedure and the Stored Procedure is executed using the EXEC command in SQL Server.

Why set Nocount on is used in SQL?

SET NOCOUNT ON prevents the sending of DONEINPROC messages to the client for each statement in a stored procedure.


2 Answers

You can change the stored procedure to accept a table valued parameter as an input. First however, you will need to create a user defined table TYPE which matches the structure of the C# DataTable:

CREATE TYPE dbo.PersonType AS TABLE
(
    Name NVARCHAR(50), -- match the length of SomeTable.Column1
    Age INT
);

Adjust your SPROC:

CREATE PROCEDURE dbo.InsertPerson
    @Person dbo.PersonType READONLY
AS
BEGIN
  INSERT INTO SomeTable(Column1, Column2) 
     SELECT p.Name, p.Age
     FROM @Person p;
END

In C#, when you bind the datatable to the PROC parameter, you need to specify the parameter as:

parameter.SqlDbType = SqlDbType.Structured;
parameter.TypeName = "dbo.PersonType";

See also the example here Passing a Table-Valued Parameter to a Stored Procedure

like image 198
StuartLC Avatar answered Oct 26 '22 18:10

StuartLC


First you need to create a Userdefined type of table that resembles your actual table. See the example below,

CREATE TYPE SomeType AS TABLE 
(
   C1 int, 
   C2 VARCHAR(50)
)
After this you need to create a stored procedure that takes this table type   as parameter.


CREATE PROCEDURE SomeUSP
  @tabType SomeType READONLY
AS
BEGIN
   SET NOCOUNT ON;

   INSERT INTO Yourtable(C1,C2)
   SELECT C1,C2 FROM @tabType 
END

That's it...Job done :)

like image 43
Shyju Avatar answered Oct 26 '22 19:10

Shyju