Wrox Programmer Forums
|
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
 
Old October 8th, 2004, 02:30 AM
Registered User
 
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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:)
 
Old October 8th, 2004, 03:00 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

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.
 
Old October 8th, 2004, 01:25 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
 
Old October 8th, 2004, 08:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old September 25th, 2006, 09:09 AM
Registered User
 
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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')')





 
Old September 25th, 2006, 09:44 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old January 15th, 2007, 03:17 AM
Registered User
 
Join Date: Oct 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old January 15th, 2007, 03:18 AM
Registered User
 
Join Date: Oct 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And also does anyone knows how to optimize this? Is there another work around on this?

 
Old January 17th, 2007, 04:03 AM
Registered User
 
Join Date: Oct 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old January 17th, 2007, 06:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can also replace
Code:
EXECUTE SP_EXECUTESQL @sql
with
Code:
Exec (@sql)
Cheers.

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using sp_execute with OpenQuery and Parameters BCullenward SQL Language 0 October 27th, 2008 03:48 PM
OPENQUERY Problem tsimsha SQL Server 2005 0 October 18th, 2007 06:18 AM
OPENQUERY vs EXECUTE on a linked server? aaqqqa SQL Server 2005 1 May 28th, 2007 06:52 AM
Openquery statements aven SQL Server 2000 2 January 11th, 2006 01:33 AM
openquery gr_chris SQL Server 2000 0 September 16th, 2005 08:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.