Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access table variable in stored procedure

I am passing a datatable from Code to stored procedure in this way.

DataTable table = CommonFunctions.ToDataTable(request);
object[] spParams = new object[1];
spParams[0] = table;

DbCommand dbCommand = 
  db.GetStoredProcCommand("OS_UpdateOrgStructureDetails", spParams);

I am trying to access this parameter in stored proc.

CratePROCEDURE OS_UpdateOrgUnits  
 @table OS_RenameNodeTable READONLY  
AS  
BEGIN  
    UPDATE OrgUnit  
    SET DetailName = ut.NewValue  
    FROM @table ut  
    INNER JOIN OrgUnit ou ON ou.OrgUnitID = ut.OrgUnitID  
END 

But when the call is made to stored procedure it throws an error.

The incoming tabular data stream (TDS) remote procedure call (RPC) protocol 
stream is incorrect. Table-valued parameter 1 ("@table"), row 0, column 0: 
Data type 0xF3 (user-defined table type) has a non-zero length database name 
specified.  Database name is not allowed with a table-valued parameter, only 
schema name and type name are valid.

Not able to resolve the error.

like image 442
ankur Avatar asked Mar 29 '12 07:03

ankur


People also ask

How can I pass a table variable in stored procedure?

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. Inside the stored procedure, select the data from the passed parameter and insert it into the table that you want to populate.

Can you declare a table as a variable in SQL?

Table variable is a type of local variable that used to store data temporarily, similar to the temp table in SQL Server. Tempdb database is used to store table variables. To declare a table variable, start the DECLARE statement. The name of table variable must start with at(@) sign.


2 Answers

Because of a bug in the SqlCommandBuilder.DeriveParameters method, the TypeName property of the SqlParameter object for the table valued parameter contains the database name (see http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.deriveparameters.aspx, the comment "Table valued parameters not typed correctly").

The fix this you can add this general purpose code right after creating the command:

foreach (SqlParameter parameter in dbCommand.Parameters)
{
    if (parameter.SqlDbType != SqlDbType.Structured)
    {
        continue;
    }
    string name = parameter.TypeName;
    int index = name.IndexOf(".");
    if (index == -1)
    {
        continue;
    }
    name = name.Substring(index + 1);
    if (name.Contains("."))
    {
        parameter.TypeName = name;
    }
}
like image 194
tdracz Avatar answered Sep 28 '22 14:09

tdracz


If you only have one or two table parameters, you don't have to loop through all the parameters. I wrote a function instead and passed that parameter to that function so that it would fix the typename.

This is the function:

    Private Sub SetTypeNameForTableParameter(ByRef parameter As System.Data.SqlClient.SqlParameter)
        If parameter.SqlDbType = SqlDbType.Structured Then
            Dim name As String = parameter.TypeName
            Dim index As Integer = name.IndexOf(".")
            If index <> -1 Then
                name = name.Substring(index + 1)
                If name.Contains(".") Then
                    parameter.TypeName = name
                End If
            End If
        End If
    End Sub

This is the piece of code where I'm making the call to the database:

'Get Parameters in stored proc
            Dim cmd As System.Data.Common.DbCommand = db.GetStoredProcCommand("MyStoredProc")
            db.DiscoverParameters(cmd)
            'The first parameter is the return value. Remove it.
            Dim returnValueParam As Data.Common.DbParameter = cmd.Parameters(0)
            cmd.Parameters.Remove(returnValueParam)
            'Set type name for every table parameter
            SetTypeNameForTableParameter(cmd.Parameters(1))
            'Assign values to the parameters
            cmd.Parameters(0).Value = id
            cmd.Parameters(1).Value = mydatatable
            'Execute the command
            db.ExecuteNonQuery(cmd)
like image 39
Silvia D. Avatar answered Sep 28 '22 16:09

Silvia D.