2016-02-03

TSQL - Parameterized openquery using a linked server (example)

declare @prmReportingDate as date;
set @prmReportingDate = '2016-02-03';
declare @sql as varchar(8000);
set @sql = '
       select
       *
       from
              OPENQUERY(
                     LINKED_SERVER_NAME_HERE
                     , ''
                           select
                                  FIELD_NAME_1
                                  , FIELD_NAME_2
                           from
                                  SOURCE_TABLE_NAME
                           where
                                  FILTER_FIELD_1 = cast(''''' + cast(@prmReportingDate as varchar(10)) + ''''' as date)
                           ;
                     ''
              ) --openquery end
       ;
';
print(@sql);
exec(@sql);


No comments:

Post a Comment