Question

I have the following query I'm executing against a remote SQL Server:

EXEC LinkedServer.MyDatabase..sp_executesql N'SELECT sys.fn_cdc_get_min_lsn(''dbo_td_coater_rga_new'');';

The query works fine, but I'm not sure how to capture the results of the query and stored it to a variable. For example, this does not work:

Declare @begin_lsn binary(10)

Set @begin_lsn = EXEC LinkedServer.MyDatabase..sp_executesql N'SELECT sys.fn_cdc_get_min_lsn(''dbo_td_coater_rga_new'');';
Was it helpful?

Solution

You can pass the variable to sp_executesql. You have to declare it as OUTPUT to receive the value back:

Declare @begin_lsn binary(10)

EXEC LinkedServer.MyDatabase..sp_executesql N'SELECT @begin_lsn = sys.fn_cdc_get_min_lsn(''dbo_td_coater_rga_new'');', 
    N'@begin_lsn binary(10) OUTPUT', @begin_lsn OUTPUT
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top