 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 8th, 2004, 02:30 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
openquery syntax
hi,
i am using openquery to write the select statement,that query i am giving below..but it is
giving syntax error..:(
EXEC ('SELECT * FROM OPENQUERY ( ITCCUST, ''SELECT REF_NO7 from GL_BATCHDETAILACCT where
PERIOD_NO>=' ' ' ' ' +@StartPeriod+' ' ' ' ' +and PERIOD_NO<=+ ' ' ' ' '+@EndPeriod+' ' ' ' '
+and FISCAL_YR>=+' ' ' ' ' +@StartYr+' ' ' ' ' +and FISCAL_YR <=+' ' ' ' ' +@EndYr+' ' ' ' ' '
' )')
can anybody tell me the write syntax..
thanks in advance:)
|
|

October 8th, 2004, 03:00 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What do you mean by this delimiter ' ' ' ' ' ?
AFAIK, you need only use ' and ' with any possible escape character while using openquery.
I have not used the arguments passed to the query like @StartPeriod. I doubt whether this is possible.
|
|

October 8th, 2004, 01:25 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
The query must be a string literal, it can't be built up dynamically. You need to filter outside the openquery part.
Code:
SELECT * FROM OPENQUERY(<main select here>) WHERE ...
--
Joe
|
|

October 8th, 2004, 08:48 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Did you check on BOL? Here is the syntax...
Quote:
quote:Syntax
Code:
OPENQUERY(linked_server, 'query')
Arguments
linked_server
Is an identifier representing the name of the linked server.
'query'
Is the query string executed in the linked server.
Examples
This example creates a linked server named OracleSvr against an Oracle database using the Microsoft OLE DB Provider for Oracle. Then this example uses a pass-through query against this linked server.
Note: This example assumes that an Oracle database alias called ORCLDB has been created.
Code:
EXEC sp_addlinkedserver 'OracleSvr',
'Oracle 7.3â,
'MSDAORA',
'ORCLDB'
GO
SELECT *
FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM joe.titles')
GO
|
Hope that helps.
Cheers!
_________________________
- Vijay G
Strive for Perfection
|
|

September 25th, 2006, 09:09 AM
|
|
Registered User
|
|
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What about
select * from mytable where item like ('A','B')
That's a problem for me, how do i type it
open OPENQUERY(xxx,'select * from mytable where item like ('A','B')')
|
|

September 25th, 2006, 09:44 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
OPENQUERY replaces a table so you need something like:
Code:
SELECT * FROM OPENQUERY(xxx,'select * from mytable ') where item in ('A','B')
--
Joe ( Microsoft MVP - XML)
|
|

January 15th, 2007, 03:17 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Does this mean if I have a million of data from the linked server, it would first be transported to where my code executes then after that it would filter the transferred data?
A million of records first be transported to my server before the where clause would take an effect?
Is this correct?
|
|

January 15th, 2007, 03:18 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
And also does anyone knows how to optimize this? Is there another work around on this?
|
|

January 17th, 2007, 04:03 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I got a solution for my problem. I used SP_EXECUTESQL of SQL server.
Just incase you want to know how I solved my problem. :)
Code:
DECLARE @sql NVARCHAR(4000)
DECLARE @number INT
SET @number = 1
SET @sql = 'SELECT * FROM OPENQUERY(TEST_LINK,
''SELECT * FROM table1
WHERE stringField = ' + '''''' + convert(varchar,@number) + '''''' +
' AND numericField = ' + convert(varchar,@number) + ' '')'
PRINT @sql
EXECUTE SP_EXECUTESQL @sql
Just take note of the single quotes inside the query (@sql).
Hope this helps. And anyone who has a better solution please let me know.
Thanks.
|
|

January 17th, 2007, 06:33 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
You can also replace
Code:
EXECUTE SP_EXECUTESQL @sql
with
Cheers.
_________________________
- Vijay G
Strive for Perfection
|
|
 |