Send Results of Stored Procedure to Temp Table

Frank Anellia 41 Reputation points
2021-03-31T13:51:15.153+00:00

Hello,

I'm trying to programmatically monitor transactional replication of several SQL databases. So far, I have the following code snippet but it seems to be missing something:

SELECT * INTO #PublisherInfo
FROM OPENROWSET('SQLOLEDB', 
                'SERVER=servername;TRUSTED_CONNECTION=YES;',
                'SET FMTONLY OFF; SET NOCOUNT ON; EXEC distribution.dbo.sp_replmonitorhelppublisher WITH RESULT SETS
(publisher, distribution_db, status, warning, publicationcount, returnstamp)')

But I'm receiving the following error:

OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Deferred prepare could not be completed.".
Msg 8180, Level 16, State 1, Line 12
Statement(s) could not be prepared.
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near 'publisher'.

Any help would be appreciated. Thx!

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,696 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Guoxiong 8,126 Reputation points
    2021-03-31T14:36:30.92+00:00

    Try this:

    SELECT * INTO #PublisherInfo
    FROM OPENROWSET(
        'SQLNCLI11', 
        'SERVER=localhost;TRUSTED_CONNECTION=YES;',
        'SET FMTONLY OFF; SET NOCOUNT ON; EXEC distribution.dbo.sp_replmonitorhelppublisher 
         WITH RESULT SETS (
            (
            publisher varchar(20), 
            distribution_db varchar(20), 
            status INT, warning INT, 
            publicationcount INT, 
            returnstamp varchar(20)
            )
        )'
    );
    

2 additional answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-03-31T13:58:09.197+00:00

    It seems that WITH RESULT SETS requires datatypes, for example: WITH RESULT SETS (([publisher] varchar(max), [distribution_db] char(10), …)). Try specifying the appropriate types and use additional '( )'.

    0 comments No comments

  2. Frank Anellia 41 Reputation points
    2021-03-31T14:17:57.943+00:00

    So I'm able to run the following with your change:

    EXEC distribution.dbo.sp_replmonitorhelppublisher
    WITH RESULT SETS
    (
    (publisher varchar(20), distribution_db varchar(20), status INT, warning INT, publicationcount INT, returnstamp varchar(20))
    )

    However, if I run it in its entirety...

    SELECT * INTO #PublisherInfo
    FROM OPENROWSET('SQLOLEDB', 
                    'SERVER=servername;TRUSTED_CONNECTION=YES;',
                    'SET FMTONLY OFF; SET NOCOUNT ON; EXEC distribution.dbo.sp_replmonitorhelppublisher WITH RESULT SETS
                    ((publisher varchar(20), distribution_db varchar(20), status INT, warning INT, publicationcount INT, returnstamp varchar(20))')
    

    I still receive the following error:

    OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Deferred prepare could not be completed.".
    Msg 8180, Level 16, State 1, Line 12
    Statement(s) could not be prepared.
    Msg 102, Level 15, State 1, Line 13
    Incorrect syntax near ')'.