0

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'');';
Hosea146
  • 7,412
  • 21
  • 60
  • 81

1 Answers1

2

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
Szymon
  • 42,577
  • 16
  • 96
  • 114