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 January 17th, 2007, 11:19 AM
Authorized User
 
Join Date: Dec 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT * FROM OPENQUERY(xxx,'select * from mytable ') where item in ('A','B')
The above query is a poorly performing method. You should let the remote server handle as much processing as the business will allow. (i.e. SELECT * FROM OPENQUERY(xxx, 'select field1, field2 from mytable where item in (''A'',''B'')') ) This query will allow the remote server to only send over the filtered records.

Local testing, on a table with 241000 rows, shows a >60% performance increase by remote filtering.

Now, I should qualify that OPENQUERY can only be used if the query has been predetermined and fixed. If you need use dynamic sql or use r-value parameters, sp_executesql or exec should be used. In either case, let the remote server do the work and always benchmark before you release.

Adam Gossage
Lake Wylie, SC, USA
 
Old March 15th, 2010, 01:27 AM
Registered User
 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically writing OpenQuery with parameters

here is another solution:

Just a note: best to use the sp_executesql method as opposed to just plain Exec() as sp_executesql 's execution plans are inserted into the procedure cache and can be reused. Also get do some of the stuff below:

--Dynamically writing a Select string from mysql using an output parameter
DECLARE @VCArticleId VARCHAR(20), @Id INT, @SQLString nvarchar(500),
@ParmDefinition nvarchar(500)

SET @SQLString = N'SELECT @Id_Out = id FROM OPENQUERY(mylinkedserver,''Select id From jos_content where metadesc = "'+ @VCArticleId+'" ORDER BY id DESC limit 1'')'
SET @ParmDefinition = N'@Id_Out INT OUTPUT'
EXECUTE sp_executesql @SQLString, @ParmDefinition, @id_out = @Id OUTPUT;

--Then you can do what you want with @Id

You can also use the above approach with input parameters.

I also had fun and games with inserting data into joomla's mysql. Some daft programmer had used a reserved word for a field name , thus it took me ages and much pain to get the right escape character. in my case the following worked. I'm talking from mssql2005 to my sql 5

--here is the rendered SQL query I used, take note of the escape ditti's around fulltext, they ain't the normal ''
insert openquery (joomlamysql, 'select `fulltext` from jos_content')
select A.Body AS Ftext FROM mssql.dbo.table A where id = @articleid

--so never ever use a reserved word in a field name......





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.