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 10th, 2006, 07:28 AM
Registered User
 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Openquery statements

Hi guys

Can anyone please help me. I need to know how to pass an integer value to an openquery (linked server) statement. I know how to pass a string value. But when i pass try to pass integer values i get all sorts of errors.

Thanx again

avi

 
Old January 10th, 2006, 12:27 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

OpenQuery requires the statement to be a literal and does not permit variables

Nonetheless, you could wrap the OpenQuery statement inside of a sp_ExecuteSQL call, or an Exec('') call.

-- Here is how OpenQuery normally works -- no parameters nothing generating variably
SELECT * FROM OPENQUERY(LocalServer,'SELECT * FROM pubs.dbo.Employee WHERE [job_id]> 1')

-- Here is how to wrap OpenQuery inside a sp_ExecuteSQL or Exec statement
-- We can't pass in parameters to the sp_ExecuteSQL because it will give us an error about not being
-- able to prepare the deferred query. So we must build our parameters into the SQLString
DECLARE @StrSQL nvarchar(4000)
DECLARE @jobid smallint
DECLARE @ParmDef NVARCHAR(500)

SET @jobid = 1 -- This is the parameter you are getting
SELECT @StrSQL = N'SELECT * FROM OPENQUERY(LocalServer,''SELECT * FROM pubs.dbo.Employee WHERE [job_id]> ' + CAST(@JobID as nvarchar(30)) + ''')'
EXEC sp_ExecuteSQL @StrSQL


David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com
The Following User Says Thank You to David_the_DBA For This Useful Post:
skywalker (May 20th, 2010)
 
Old January 11th, 2006, 01:33 AM
Registered User
 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx alot mr David Lundell

It works brilliantly.

Thanx Again
aven






Similar Threads
Thread Thread Starter Forum Replies Last Post
openquery syntax mpankuj SQL Server 2000 11 March 15th, 2010 01:27 AM
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 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.