Skip to content

Not catching results generated after PRINT or warning from stored procedures with NOCOUNT on #276

@mdpenguin

Description

@mdpenguin

If a statement in a stored procedure produces a record set then procedureMgr is not returning it in the results if the record set follows one of the following:

  • a PRINT statement
  • an ANSI warning
  • an error message with a severity level <= 10 (a warning rather than a halting error)

This can be replicated with this stored procedure:

CREATE PROCEDURE [dbo].[NullEliminated](@P1 TINYINT)
AS
BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #Dummy (ID TINYINT);
    
    SELECT 'First query' 'FirstQuery';

    SELECT 'Second query' 'SecondQuery';

    PRINT 'That was second; now we do third';

    SELECT 'Third query' 'ThirdQuery';

    SELECT 'Fourth query' 'FourthQuery';

    SELECT 'Fifth query' 'FifthQuery';

    INSERT INTO #Dummy(ID)
    SELECT SUM(n) 'ID'
    FROM(VALUES(1),
               (NULL)) x(n);

    INSERT INTO #Dummy(ID)
    SELECT SUM(n) 'ID'
    FROM(VALUES(1),
               (NULL)) x(n);

    SELECT 'Sixth query' 'SixthQuery';

    SELECT 'Seventh query' 'SeventhQuery';

    RAISERROR('I am warning you!', 10, 1);

    SELECT 'Eighth query' 'EighthQuery';
END

Here is the JS I was using to run the procedure:

const sql = require("msnodesqlv8");

const connectionString = "Driver={ODBC Driver 17 for SQL Server};Server=YourServerHere;Database=YourDBHere;Trusted_Connection=yes;'";

sql.open(connectionString, function (err, conn) {
    var pm = conn.procedureMgr();
    pm.callproc('NullEliminated', [10], function (err, results, output) {
        console.log(results);
    });
});

And the results I get from that are:

[ { FirstQuery: 'First query' } ]
[]
[ { SecondQuery: 'Second query' } ]
[ { FourthQuery: 'Fourth query' } ]
[]
[ { FifthQuery: 'Fifth query' } ]
[]
[ { SeventhQuery: 'Seventh query' } ]

Note the following:

  • The third query fails to show after the PRINT statement
  • The sixth query fails to show after a couple of ANSI warnings about nulls in aggregate functions
  • The eighth query fails to show after a warning message is raised

After removing "SET NOCOUNT ON" from the stored procedure, all eight result sets are returned as seen below, though there are still some extra empty result sets included.

[ { FirstQuery: 'First query' } ]
[]
[ { SecondQuery: 'Second query' } ]
[ { ThirdQuery: 'Third query' } ]
[ { FourthQuery: 'Fourth query' } ]
[]
[]
[ { FifthQuery: 'Fifth query' } ]
[ { SixthQuery: 'Sixth query' } ]
[]
[ { SeventhQuery: 'Seventh query' } ]
[ { EighthQuery: 'Eighth query' } ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions