Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 10th, 2006, 07:28 AM
Registered User
Join Date: Jan 2006
Location: , , .
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


Reply With Quote
  #2 (permalink)  
Old January 10th, 2006, 12:27 PM
Friend of Wrox
Join Date: Dec 2005
Location: , AZ, .
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post

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

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
Reply With Quote
The Following User Says Thank You to David_the_DBA For This Useful Post:
skywalker (May 20th, 2010)
  #3 (permalink)  
Old January 11th, 2006, 01:33 AM
Registered User
Join Date: Jan 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

Thanx alot mr David Lundell

It works brilliantly.

Thanx Again

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 08:21 PM.

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