?
Solved

How do I pass a variable in a MSSQL linked server query?

Posted on 2009-12-28
4
Medium Priority
?
485 Views
Last Modified: 2012-05-08
Hello experts,

I would like to pass a variable in a linked server query. I am using MSSQL 2008 SQL server management studio.

I am querying a linked server running a Progress database

This query works

select count('order') as 'Orders Entered' from Openquery(V2, 'select order from oe_head where ord_date = ''12/28/2009'' and company_oe = ''cc'' and rec_type = ''o''')

I would like to pass a variable DATE for the ord_date but can't seem to get the syntax correct

Any ideas?

0
Comment
Question by:pgeisler
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26131228
you can only do that with dynamic sql, as OPENQUERY does (unfortunately) not allow a variable for the SQL ...
0
 

Author Comment

by:pgeisler
ID: 26134302
could you give an example?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 26139456
this should do....
and yes, this is the "hell of quotes" :)
declare @sql varchar(1000)
declare @date varchar(20)
declare @comp varchar(20)
declare @rect varchar(20)

set @date = '12/28/2009'
set @comp = 'cc'
set @rect = 'o'

set @sql = 'select count([order]) as [Orders Entered] 
 from Openquery(V2, ''select order from oe_head where ord_date = ''''' + @date + ''''' and company_oe = '''''+ @comp + ''''' and rec_type = ''''' + @rect + ''''' '') '

Open in new window

0
 

Author Closing Comment

by:pgeisler
ID: 31670385
Perfect example, it was the quote "hell of quotes" I was having trouble with.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question