Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
OpenRowSet
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Miscellaneous
Thread ID:
00584963
Message ID:
00584974
Views:
25
I think you should use sp_executesql to dynamically execute whole query. Samething like
...
declare @Query varchar(200), @Sql varchar(200), @ParmDefinition varchar(200)
...
SET @ParmDefinition = '@QueryParam varchar(200)'
SET @Sql = 'SELECT * FROM OpenRowset( ''SQLOLEDB'', ''xxxx'';''xxxxx'';''xxxxx'',  @QueryParam)'
EXECUTE sp_executesql @Sql ), @ParmDefinition, @QueryParam = @Query 

>I want to dynamically build the query I send to the OpenRowset function something like this:
>
>declare @datehb datetime
>declare @Query varchar(200)
>
>set @datehb = (select max(date) from histobarraCNE3)
>
>set @Query = ('SELECT * FROM RQuant.dbo.histobarrause3 where date> ''' + cast(datepart(yy,@datehb) as varchar(4)) + '-' + cast(datepart(mm,@datehb) as varchar(2)) + '-' + cast(datepart(dd,@datehb) AS varchar(2)) + '''')
>--select @query
>
>SELECT * FROM OpenRowset( 'SQLOLEDB', 'xxxx';'xxxxx';'xxxxx', @Query)
>
>When I execute this, I have the error:
>Server: Msg 170, Level 15, State 1, Line 16
>Line 16: Incorrect syntax near '@Query'.
>
>How can I tell SQL to run my query?
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform