Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass table value param to stored procedure using PetaPoco

For while I am trying to call SQL Server 2008 R2 stored procedure using PetaPoco.

My stored procedure accepts a table valued parameter.

How I can call the stored procedure in petapoco with table value param?

Here what I am trying to do:

var db = new PetaPoco.Database("repikaciskaBaza");

DataTable table = new DataTable();
DataColumn id = table.Columns.Add("id", type: typeof(Int32));

for (int i = 0; i < 10;i++ )
{
    DataRow row = table.NewRow();
    row["id"] = i;
    table.Rows.Add(row);
}

var param = new SqlParameter();
param.DbType = DbType.Object;
param.ParameterName = "@art_id";

param.SqlValue = table;

var lista = db.Query<pocoArts>(";exec dbo.test_sporc_param @0", param);

This code gives me an exception :

The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.
Parameter 3 ("@0"): Data type 0x62 (sql_variant) has an invalid type for type-specific metadata.

If I set parametar ty value

param.SqlDbType = SqlDbType.Structured;

Then I get exception like

The table type parameter '@0' must have a valid type name.

When I define my param like

            param.SqlDbType = SqlDbType.Structured;
            param.SqlValue = table;
            param.ParameterName = "@art_id";
            param.TypeName = SqlDbType.Structured.ToString();

Then I get exception

Column, parameter, or variable @0. : Cannot find data type Structured.

How I can define SqlParam with table valued param so I can send it whit data to SQL Server?

Solution:

var param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured; // According to marc_s
param.SqlValue = table; 
param.ParameterName = "@art_id";
param.TypeName = "dbo.typ_art_id"; // this is TYP from SQL Server database it needs to be equal to type defined in SQL Server not type of param
like image 340
adopilot Avatar asked Jun 10 '13 08:06

adopilot


People also ask

Can we pass table as a parameter to stored procedure?

You cannot use a table-valued parameter as target of a SELECT INTO or INSERT EXEC statement. A table-valued parameter can be in the FROM clause of SELECT INTO or in the INSERT EXEC string or stored procedure.

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.

How do you pass a parameter to a procedure?

To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.


1 Answers

According to the relevant MSDN documentation on table-valued parameter, you should use:

var param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured;

The SqlDbType.Structured is the key to this. Don't use DbType.Object.

like image 71
marc_s Avatar answered Nov 05 '22 19:11

marc_s