Returning Row Counts From Procedure?

  • I have a procedure that selects records from one table to another, depending on the error flag, Int, 1 or 0.

    The procedure is defined as:

    Create Procedure InsertInstMain(@JobKey Int, @Rows Int Output, @Errors Int Output)

    As

    I am trying to populate the 2 output variables defined in the procedure create statement. I have tried both of the following statements, with and without Set NoCount On

    Set @Rows = @@RowCount

    Select @Rows = Count(*) From InstRaw Where Error = 0 And JobTableKey = @jobkey

    I do the same thing with the errors, which are inserted into another table, if some fields fail data validation.

    Problem is no values are returned from my procedure. I am beginning to think this is impossible, but perhaps I am not holding my mouth right. If I have NoCount Off I do get information back, in the form of "695,347 row(s) affected", when running in SSMS.

    This procedure will be called from an SSIS package once I get it working correctly.

    Thanks,

    Chris

    Learning something new on every visit to SSC. Hoping to pass it on to someone else.

  • It's probably how you are calling the sp. Try this in a query window:

    alter Procedure InsertInstMain(@JobKey Int, @Rows Int Output, @Errors Int Output)

    As

    SET NOCOUNT ON

    --I am trying to populate the 2 output variables defined in the procedure create statement.

    --I have tried both of the following statements, with and without Set NoCount On

    SELECT [Today] = GETDATE()

    SET @Rows = @@RowCount

    SET @Errors = 999

    RETURN 0

    GO

    DECLARE @jobkey Int, @Rows Int, @Errors Int

    EXEC InsertInstMain @jobkey, @Rows OUTPUT, @Errors OUTPUT

    SELECT [Rows] = @Rows, [Errors] = @Errors

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • That works nice. I guess I needed to specify Output when I call the proc. That works better.

    Thanks,

    Chris

    Learning something new on every visit to SSC. Hoping to pass it on to someone else.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply