Pass Table Variable as a Input parameter to Stored Procedure

Description

I know how to execute stored procedures with Parameter Name and Parameter Value using
var query= session.GetNamedQuery(StoredProcedureName);
query.SetParameter(ParameterName,ParameterValue).

But I don't know how to Pass the Table Variable(contains complete data table) as a input Parameter to the SQL Server Stored procedure using NHibernate Mapping. Can you please help me on this.

Thanks in advance. Satish

Environment

Windows and C#.Net

Activity

Show:

Alex Zaytsev 
April 23, 2015 at 11:07 AM

What about other dialects?

Ricardo Peres 
December 11, 2014 at 9:29 PM

Yes... that is precisely what my fix does...

SATISH 
December 11, 2014 at 9:07 PM

Thank you Peres, Finally I got it work.
I followed the link https://stackoverflow.com/questions/3701364/nhibernate-sqldbtype-structured

in Sql2008Structured Class , at the below method I changed the statement s.Parameters[index].TypeName = "MyTablevariableType";
public void NullSafeSet(IDbCommand st, object value, int index, NHibernate.Engine.ISessionImplementor session) {
var s = st as SqlCommand;
if (s != null) {
s.Parameters[index].SqlDbType = SqlDbType.Structured;
s.Parameters[index].TypeName = "IntTable"; -----This is my TablevariableType.
s.Parameters[index].Value = value;
}
else {
throw new NotImplementedException();
}
Then It works fine.
Thanks for your help and quick reply.

Ricardo Peres 
December 8, 2014 at 8:06 PM

But did you clone NHibernate, got my pull request, compiled NH and tested with this version?

SATISH 
December 8, 2014 at 4:48 PM

Hi Peres,

I tried with
var Mytable = new DataTable("dbo.TableType");
Mytable.Columns.Add("CategoryID", typeof(int));
Mytable.Columns.Add("CategoryName", typeof(string));
Mytable.Rows.Add(1, "TestSatish");

var result = session.CreateSQLQuery("EXEC usp_TestSatiUpdateCatgorys :t").SetParameter("t", Mytable).List();. But I am getting "Operand type clash: varbinary is incompatible with TestSatishTableType1 error.

Then i tried with other option which is var result = session.CreateSQLQuery("EXEC dbo.TableProcedure :t").SetParameter("t", Mytable, NHibernateUtil.Structured("dbo.TableType")).List(); But I am getting Compilation error NHibernateUtil class does not contains a definition for Structured.

When I check at SQl Server Side Stored procedure and Table variable are working fine. Can you please let me know where I am doing wrong.

At Database side my table variable is below
'CREATE TYPE [dbo].[TestSatishTableType1] AS TABLE(
[CategoryID] [int] NULL,
[CategoryName] [nvarchar](max) NULL
)
GO'

and my Stored Procedure is below

ALTER PROCEDURE [dbo].[usp_TestSatiUpdateCatgorys]
@TestSatish as TestSatishTableType1 READONLY
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Test_SatishTable as TestSatishTableType1
insert into dbo.TestSatishCategoryTable1 (CATID,CATNAME) select nc.CategoryID, nc.CategoryName FROM @TestSatish AS nc;
END
GO

Can you please let me know where I am doing mistake.

Thanks in advance.

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created November 18, 2014 at 7:02 PM
Updated April 23, 2015 at 12:21 PM
Who's Looking?