Question

If I have a stored procedure like this:

CREATE PROCEDURE [dbo].[sp_update_dummy]
AS
BEGIN
  update update_dummy set value = value + 1 where id = 1
END

and call this using executeUpdate (from standard java.sql library) then the updated row count is returned to the Java program (assuming, of course, that the update statement updates a row in the table).

However if I execute a CLR stored procedure coded like this:

[Microsoft.SqlServer.Server.SqlProcedure]
public static void clr_update_dummy()
{
    using (SqlConnection conn = new SqlConnection("context connection=true"))
    {
        SqlCommand command = new SqlCommand("update update_dummy set value = value + 1 where id = 1", conn);

        conn.Open();

        command.ExecuteNonQuery();

        conn.Close();
    }
}

Then the Java program does not get the updated row count (it seems to get a value of -1 returned). This is also what happens if I put SET NOCOUNT ON into the SQL stored procedure.

So it looks to me that a CLR stored procedure acts as if SET NOCOUNT ON is used.

Is there any way to code a CLR stored procedure so that row count can be picked up in the same way it is for a SQL stored procedure? Unfortunately it isn't possible to change the Java program (it is a 3rd party component) to, for example, pick up an OUTPUT parameter. I've looked at SqlContext.Pipe but there is nothing obvious there. Also I'm not sure of the mechanism by which the row count is returned to the executeUpdate procedure.

I can probably create a hack to get around the problem (Java executes a SQL stored procedure which in turn executes a CLR stored procedure for instance) but if possible I'd like to not introduce another layer into the call stack.

Was it helpful?

Solution

You probably (if it makes sense to just be turning around and running a piece of SQL from inside your CLR procedure) want to call ExecuteAndSend

In addition to any actual results, other messages and errors are also sent directly to the client.

SqlCommand command = new SqlCommand("update update_dummy set value = value + 1 where id = 1", conn);
conn.Open();

SqlContext.Pipe.ExecuteAndSend(command);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top