My problem is that the criteria for the queries I need to
pass with OPENQUERY change on each run. So far I have not
figured out how to make the query argument dynamically
change with each run. OPENQUERY according to the
documentation does not accept any type of variables.
OPENQUERY(SERVER, 'Query') how do I build the Query
danamically, I am hoping someone has seen this problem
before and discovered a soulution or work around.
Thanks,
Simon
EG:
DECLARE @sql varchar(4000)
SET @sql='SELECT * FROM OPENQUERY( ...........
EXEC (@sql)
>.
>
When you say the query changes dynamically means, only the where clause
arguments right ??
In that case you can just use the following kinda format
Select A.* from OPENQUERY(srv,'SELECT * from Table') A
Where A.col = @var
Please let me know if that helps.
Thanks,
Vikrant Dalwale
Microsoft SQL Server Support Professional
This posting is provided "AS IS" with no warranties, and confers no rights.
Get secure !! For info, please visit http://www.microsoft.com/security.
Please reply to Newsgroups only.
--------------------
| Content-Class: urn:content-classes:message
| From: "Simon McLaren" <simon....@cnet.navy.mil>
| Sender: "Simon McLaren" <simon....@cnet.navy.mil>
| Subject: OPENQUERY with a dynamic query
| Date: Mon, 1 Jul 2002 06:19:48 -0700
| Lines: 19
| Message-ID: <140a201c22101$f9b29730$19ef2ecf@tkmsftngxa01>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcIhAfmyDcheYlVwTL6eU44fpgX+xQ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.sqlserver.programming
| NNTP-Posting-Host: TKMSFTNGXA01 10.201.232.160
| Path: cpmsftngxa08!cpmsftngxa06!cpmsftngxa07
| Xref: cpmsftngxa08 microsoft.public.sqlserver.programming:270486
| X-Tomcat-NG: microsoft.public.sqlserver.programming
The query works fine in Query analyzer with the 2 single
quotes are used around the fields. But when I have to put
it into a variable I get an error (Incorrect syntax
near '2002'.) If I print out this statement it is
returning single quotes. I've tried every possible
permutation of single and double quotes but can't seen to
get the concatenation out properly.
Anyone working on something like this?
Any hints would be appreciated.
Thanks
Norma
**************************************************
....
declare @mySQL varchar(500)
set @mySQL = 'SELECT * INTO ' + @sTable + '
FROM OPENQUERY(SERV01,
''SELECT SampleCode, Date, Value
FROM LIMSTST.dbo.Result
WHERE Date BETWEEN { ts ''2002-05-08 14:45:01''} AND {
ts ''2002-05-31 00:00:00'' }
AND (ParameterCode =''CL2Free'')
ORDER BY SampleDT'')'
exec (@mySQL)
***********************************************
>.
>
Norma (nb...@gvrd.bc.ca) writes:
> **************************************************
> ....
> declare @mySQL varchar(500)
>
> set @mySQL = 'SELECT * INTO ' + @sTable + '
> FROM OPENQUERY(SERV01,
> ''SELECT SampleCode, Date, Value
> FROM LIMSTST.dbo.Result
> WHERE Date BETWEEN { ts ''2002-05-08 14:45:01''} AND {
> ts ''2002-05-31 00:00:00'' }
> AND (ParameterCode =''CL2Free'')
> ORDER BY SampleDT'')'
>
> exec (@mySQL)
>
> ***********************************************
You have an opening delimiter at SELECT which you close at 2002. Let's
see, I believe you need to quadruple the quotes at that point. This
brought me as far as complaints on ANSI_NULLS and ANSI_WARNINGS not
being set, so I guess I got past the quote issues:
declare @mySQL varchar(500), @sTable sysname set @sTable = '#nnn'
set @mySQL = 'SELECT * INTO ' + @sTable + '
FROM OPENQUERY(SERV01,
''SELECT SampleCode, Date, Value
FROM LIMSTST.dbo.Result
WHERE Date BETWEEN { ts ''''2002-05-08 14:45:01''''} AND {
ts ''''2002-05-31 00:00:00'''' }
AND (ParameterCode =''''CL2Free'''')
ORDER BY SampleDT'')'
exec (@mySQL)
--
Erland Sommarskog, SQL Server MVP
som...@algonet.se
Books Online (updated!) for SQL 2000 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp