Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 November 22nd, 2004, 03:56 PM
Authorized User
 
Join Date: Nov 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pass variable to linked server query

I'm having problem with an OpenQuery statement in stored procedure, which should be run on FoxPro linked server. I'm getting either an error messages or not the result I expect. I know that the problem is with WHERE part, without this part it works.


Here is the code:
-------------------------------------
DECLARE @LastDate datetime
SELECT @LastDate = MAX(DateChaged)
FROM tblPersonel
WHERE ACTIVE IN( 'A', 'T')

1. I tried:
SELECT * FROM OPENQUERY(FoxProLink, 'SELECT ACTIVE, EmpNumber FROM tblPersonel WHERE DateChanged >=''+@LastDate+''')

This line gives me an error msg:

Could not execute query against OLE DB provider 'MSDASQL'.
[OLE/DB provider returned message: [Microsoft][ODBC Visual FoxPro Driver]Operator/operand type mismatch.]


2. I tried to use CTOD() - FOXPRO function to convert character to date.

SELECT * FROM OPENQUERY(FoxProLink, 'SELECT ACTIVE, EmpNumber FROM tblPersonel WHERE DateChanged >=CTOD(''+@LastDate+'')')

-this doesn't give any error, but doesn't limit the result set as it should.
 
Old November 23rd, 2004, 08:30 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

Hi,

I feel the problem is in Passing Parameter. You have defined @LastDate
 as DateTime and trying to pass it in the way of Varchar/Char. You should use CAST / Convert to change the datatype into Varchar.
Here it goes :-

DECLARE @LastDate datetime
SELECT @LastDate = MAX(DateChaged)
FROM tblPersonel
WHERE ACTIVE IN( 'A', 'T')

SELECT * FROM OPENQUERY(FoxProLink, 'SELECT ACTIVE, EmpNumber FROM tblPersonel WHERE DateChanged >='+ CONVERT(VARCHAR(10),@LastDate,110)+'''')

Hope this works.

Gud luck

B. Anant
 
Old November 23rd, 2004, 08:37 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI again,

If your second solution works but giving wrong info, try to use CONVERT to remove time part from @LastDate.

SELECT * FROM OPENQUERY(FoxProLink, 'SELECT ACTIVE, EmpNumber FROM tblPersonel
WHERE
DateChanged >=CTOD(''+Convert(Varchar(10),@LastDate,110)+'') ')

Gud luck



B. Anant
 
Old November 23rd, 2004, 11:41 AM
Authorized User
 
Join Date: Nov 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It didn't work, for OpenQuery I need to use FoxPro functions, since it runs on linked server.
Thanks for your reply.
 
Old November 23rd, 2004, 02:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Sofya,

What is the value that comes with @LastDate? You have declared that as datetime, why don't you try declaring it as varchar(10) and pass the value in the right format?

Or try this.
Code:
Declare @strOpenQuery varchar(200)
Select @strOpenQuery = 'SELECT ACTIVE, EmpNumber  FROM tblPersonel WHERE DateChanged >=CTOD(''+ @LastDate + '')'
-- Display @strOpenQuery to see if the value gets substituted fine.
Select @strOpenQuery

SELECT * FROM OPENQUERY(FoxProLink, @strOpenQuery)
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
CommandTimeout on query using linked server sjungers Classic ASP Databases 1 February 25th, 2008 03:54 AM
How to pass javascript variable to server side Andraw Classic ASP Basics 17 January 22nd, 2007 01:05 PM
pass java variable to xsl variable kathy1016cats XSLT 1 June 14th, 2006 06:23 PM
pass the result of a query to a variable ... eusebio Pro VB Databases 6 March 30th, 2006 07:41 PM
Can't pass query string variable on secure server gnilly BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 August 24th, 2005 11:18 AM





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