x
login about faq Site discussion (meta-askssc)

How to Use Open Query in a stored procedure on a linked server

I am trying to use open query in a stored procedure to retrieve data from an AS400 linked server. It was suggested that I might need to create temp tables, then insert the data into them. Here is the basic query:

SELECT     Consultant.ConsultantID
, Consultant.FirstName
, Consultant.LastName
, cast(StartDate as varchar(12))as [Start Date]
, Consultant.CareerLevel, Sales.RetailSales
, Sales.RetailSalesTotal
, Sales.Unit
, Unit.UnitName
,Orders.OrderDate
,Orders.OrderType
,Consultant.AccountStatus

FROM         Consultant INNER JOIN
                      Sales ON Consultant.ConsultantID = Sales.ConsultantID INNER JOIN
                      Unit ON Consultant.UnitID = Unit.UnitID
WHERE StartDate between 'Jan 1 2010' and 'Dec 31 2010'

ORDER BY Sales.RetailSalesTotal desc
more ▼

asked Sep 27 '12 at 08:42 PM in Default

natongm gravatar image

natongm
108 7 11 14

Is that the query that you'll be running on the AS400? Do you have a linked server setup already?

Sep 27 '12 at 10:48 PM SirSQL
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

you can write: create table #temp ( ConsultantID int, FirstName varchar(50), LastName varchar(50), StartDate varchar(12), CareerLevel varchar(50),

RetailSales varchar(50),

RetailSalesTotal int, Unit char(10), UnitName char(10),

OrderDate datetime, OrderType char(10), AccountStatus char(10),

) insert into #temp select SELECT a.ConsultantID , a.FirstName , a.LastName , cast(a.StartDate as varchar(12))as [Start Date] , a.CareerLevel, Sales.RetailSales , Sales.RetailSalesTotal , Sales.Unit , Unit.UnitName ,Orders.OrderDate ,Orders.OrderType ,Consultant.AccountStatus from AS400.[DATABASE NAME].dbo.Consultant a INNER JOIN AS400.[DATABASE NAME].dbo.Sales ON a.ConsultantID = Sales.ConsultantID INNER JOIN AS400.[DATABASE NAME].dbo.Unit ON a.UnitID = Unit.UnitID WHERE a.StartDate between 'Jan 1 2010' and 'Dec 31 2010'

ORDER BY a.Sales.RetailSalesTotal desc

more ▼

answered Oct 26 '12 at 09:26 AM

dona gravatar image

dona
30 1 2

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x92
x26

asked: Sep 27 '12 at 08:42 PM

Seen: 588 times

Last Updated: Oct 26 '12 at 09:26 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.