I'm trying to pull back some data via a linked server that has 'Geography' fields present, consequntly I'm trying to use Open Query. I'm also trying to pass in a variable...
Can anyone explain this: This is my sql...
DECLARE @Sql VARCHAR(200)
DECLARE @tnum VARCHAR(20)= 'abc';
SET @Sql = 'SELECT * FROM NationalPolygon.dbo.Polygon WHERE TitleNumber = ''''' + @tnum + '''''';
SET @Sql = 'SELECT * FROM OPENQUERY(mylinkedserver01, ''' + REPLACE(@Sql, '?', '''') + ''')'
SELECT @Sql;
EXEC @Sql;
If I select @Sql and run it, it works. If I run the @Sql via EXEC it fails with:
Database 'SELECT * FROM OPENQUERY(mylinkedserver01, 'SELECT * FROM NationalPolygon' does not exist. Make sure that the name is entered correctly.
Thanks C