Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
SQL Server GroupsView
SQL Server Ce
SQL Server Clients
SQL Server Connect
SQL Server Datamining
SQL Server Datawarehouse
SQL Server Dts
SQL Server Fulltext
SQL Server Msde
SQL Server Olap
SQL Server Replication
SQL Server Reportingsvcs
SQL Server Security
SQL Server Server
SQL Server Setup
SQL Server Tools
SQL Server Clustering
SQL Server Programming
SQL Server Xml
SQL Server Newusers
SQL Server Integrationsvcs

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Apps
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft SQL Server Programming Posts  Ask A New Question 

calling a variable...in openquery..

Hassan posted on Saturday, January 12, 2008 7:31 PM

I want to get a value from a sql server table and then use that value into
an openquery call to a Mysql database... something like before.. but it
doesnt work..

declare @testid int

select @testid= max(id) from table1

select * from openquery (mysqlhost,'select *  from mysqltable where id > ' +
@testid )
reply

 

You have to convert the int to a varchar before concatenating its value with

Sylvain Lafontaine posted on Saturday, January 12, 2008 8:10 PM

You have to convert the int to a varchar before concatenating its value with
the rest of the string:

select * from openquery (mysqlhost, 'select *  from mysqltable where id > '
+ convert (varchar (20), @testid))

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)
reply

HassanYou've been around long enough now to know better than to just say "it

Kalen Delaney posted on Saturday, January 12, 2008 8:40 PM

Hassan

You've been around long enough now to  know better than to just say "it
doesn't work".
Did you get the wrong answer? Did you get an error message? If so, what did
it say?

--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://blog.kalendelaney.com
reply

calling a variable...in openquery..

Hassan posted on Saturday, January 12, 2008 11:05 PM

It says

Incorrect syntax near '+'.
reply

Did you try converting the variable to a varchar as Sylvain suggested?

Kalen Delaney posted on Sunday, January 13, 2008 1:51 AM

Did you try converting the variable to a varchar as Sylvain suggested?

--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://blog.kalendelaney.com
reply

calling a variable...in openquery..

Uri Dimant posted on Sunday, January 13, 2008 2:25 AM

Hassan
DECLARE @SQLx VARCHAR(500)
DECLARE @var VARCHAR(20)
SET @var = 'abcd'
SET @SQLx = 'SELECT * FROM OPENQUERY(Server,
''EXEC pubs.dbo.sp2 ''''' + @var + ''''''')'
EXEC(@SQLx)
reply

calling a variable...in openquery..

Erland Sommarskog posted on Sunday, January 13, 2008 6:59 AM

Hassan (hassan@hotmail.com) writes:

OPENQUERY does not accept an expression for the query. It has to be a
constant. This is because, SQL Server actually attempts to compile a plan
for the complete query, which is kind of difficult if the remote
query is unknown. (Hey, Kalen, you should have been around long enough
to know this. :-)

This leads to dynamic SQL, and in an orgy of nested quotes which
easily can drive you crazy. Uri's post looks scary, and but that is
just the beginning.

If you don't want to join the query reesult with something else, EXEC AT
is easier to use:

EXEC('SELECT * FROM mysqltable WHERE id > ?', @testid) AT mysqlhost

The ? is a placeholder for a parameter.

If you need to use OPENQUERY, have a look at
http://www.sommarskog.se/dynamic_sql.html#OPENQUERY for some tips for
mitigating the quote craze.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
reply

Hey ErlandLooks scary in terms of SQL Injection or nested quotes ?

Uri Dimant posted on Sunday, January 13, 2008 7:10 AM

Hey Erland

Looks scary in terms of SQL Injection or nested quotes ? :-)
reply

calling a variable...in openquery..

Erland Sommarskog posted on Sunday, January 13, 2008 9:32 AM

Uri Dimant (urid@iscar.co.il) writes:

I was mainly thinking of the nested quotes, but you are right that
SQL injection is a risk too, if the input comes from unknown sources.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
reply

Yeah, yeah...

Kalen Delaney posted on Sunday, January 13, 2008 1:09 PM

Yeah, yeah... I've done very little work with linked servers so that data
was removed from cache memory long ago!
;-)
--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://blog.kalendelaney.com
reply

calling a variable...in openquery..

Alex Kuznetsov posted on Tuesday, January 15, 2008 10:51 PM

Alternatively, this leads to implementing this functionality on the
client, which is typically much easier.
reply


Previous Microsoft SQL Server Programming conversation.