Search code examples
sql-serverstored-proceduresmetadatasql-server-2017

Is there a way to exclude the the metadata columns result when using sp_prepare


Is there a way to exclude the returned metadata columns when calling sp_prepare?

Example:

CREATE OR ALTER PROCEDURE Test
AS
-- Prepare query
DECLARE @P1 int;  
EXEC sp_prepare @P1 output,   
    N'@Param int',  
    N'SELECT 1 as int;';

-- Return handle for calling application
SELECT @P1;

-- Unprepare the query
EXEC sp_unprepare @P1;

GO

EXEC Test

The following will return two result sets, the first bieng the metadata, and the second being the desired result. Is there a way to exclude the metadata result?


Solution

  • On method to avoid returning the result set to the client is by using INSERT...EXEC to insert the result set into a table variable or temp table:

    CREATE OR ALTER PROCEDURE Test
    AS
    -- Prepare query
    DECLARE @P1 int;  
    DECLARE @trash TABLE(int int);
    INSERT INTO @trash
        EXEC sp_prepare @P1 output,   
            N'@Param int',  
            N'SELECT 1 as int;';
    
    -- Return handle for calling application
    SELECT @P1;
    
    -- Unprepare the query
    EXEC sp_unprepare @P1;
    GO