开发者

Stored Procedures with arguments

I can't make my stored procedure (SQL Server) run with NHibernate. I have following mapping:

<sql-query name="udp_MyStoredProc">
    <query-param name="Id" type="Int32" />
    <query-param name="Name" type="String" />
    <return-scalar column="ResId" type="Int32"/>
    EXEC udp_MyStoredProc @Id = :Id, 
        @Name = :Name    
</sql-query>

Within code:

IQuery query = _HibSession.GetNamedQuery("udp_MyStoredProc");
query.SetInt32("Id", id);
query.SetString("Name", name);
var res = query.List();

This results in following exception:

[SQL: EXEC udp_MyStoredProc @Id = @p0, @Name = @p1] ---> System.Data.SqlClient.SqlException: Procedure or function udp_MyStoredProc has too many arguments specified.

I don't know what's wrong with the arguments? I also read something about '?' as placeholders for the parameters, but I don't quite understand it.

Signature of the Stored Procedure:

ALTER PROCEDURE [dbo].[udp_MyStoredProc]
(
    @Id int,
    @Name NVARCHAR(255)
)
AS
BEGIN  ... END

If I do something like this within Server Management Studio, it works:

EXEC udp_MyStoredProc  @Id = 30, @Name = 'te开发者_开发技巧st'

thx for any tipps.

sl3dg3


I would recommend that you switch on debugging and actually check what the extra argument is. You should be able to see exactly which field is the problem. It may be enough to look at the generated SQL, or you may have to configure a log4net logger (log4net is the debugger that NHibernate uses internally).

To display the generated SQL in the console, add (or modify) the following property to your NHibernate config file inside the session-factory section:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
  ...
  ...
  <property name="show_sql">true</property>
</session-factory>

EDIT: I can add more details on Log4Net if you need it :)

EDIT 2: Other things to check if you get this error

  1. Your SP should only return a single value
  2. The name of the column returned by your SP should be the same as the name of the column defined in your return-scalar mapping. You can cast the column, eg:

    SELECT field to return AS ResId

EDIT 3:

you could try changing your mapping file to:

 <sql-query name="udp_MyStoredProc">
     <return-scalar column="ResId" type="Int32"/>
     EXEC udp_MyStoredProc: ?, ?    
 </sql-query>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜