Hello,
I need to join a table in my current server to a table from a linked server but I'm not sure of the sytnax. I've done stored procedures where I've done nested queries and linked tables all from the linked sever side, but I've never had to do both in one query. I was wondering if it was even possible, and if it is, what would the syntax be? I've tried several things but the sql syntax is a little different in the linked server since it's a firebird server.
Here is the stored procedure I'm working off of (this query is pulling from the linked sever) and I need to join a table from my SQL server called "Site" instead of the second query I have here.
So the top Query ("A") will be from the linked server, and the bottom query ("B") will be from my local SQL server, and I will be joining them by the column "Site."
Thanks!
I need to join a table in my current server to a table from a linked server but I'm not sure of the sytnax. I've done stored procedures where I've done nested queries and linked tables all from the linked sever side, but I've never had to do both in one query. I was wondering if it was even possible, and if it is, what would the syntax be? I've tried several things but the sql syntax is a little different in the linked server since it's a firebird server.
Here is the stored procedure I'm working off of (this query is pulling from the linked sever) and I need to join a table from my SQL server called "Site" instead of the second query I have here.
So the top Query ("A") will be from the linked server, and the bottom query ("B") will be from my local SQL server, and I will be joining them by the column "Site."
Thanks!
Code:
@BeginDate VarChar(12),
@EndDate VarChar(12),
@ItemName Varchar(25)
AS
BEGIN
declare @sql varchar(max)
set @sql='
Select
SiteName,
Name,
ItemName,
sum(Qty) as Qty,
sum(Amt) as Amt
FROM OPENQUERY
(
LinkedServer, ''
Select
B.SiteName,
A.Name,
B.ItemName,
B.Qty,
B.Amt
FROM
(
SELECT SALE.SITE as Site,
SALEITEMS.ITEM as Item,
ITEM.NAME as Name,
Sale.OBJID as SaleID,
SALEITEMS.QTY as Qty,
SALEITEMS.AMT as Amt
FROM SALE INNER JOIN SALEITEMS ON (SALE.OBJID = SALEITEMS.SALEID) AND (SALE.SITE = SALEITEMS.SITE)
INNER JOIN ITEM ON (SALEITEMS.ITEM = ITEM.OBJID)
WHERE (SALE.LOGDATE BETWEEN (''''' + CONVERT(char(10), @BeginDate, 101) + ''''') AND (''''' + CONVERT(char(10), @EndDate, 101) + ''''') )
AND Item.Name = ('''''+@itemname+''''')
) as A
,
(
SELECT SALE.SITE as Site,
SITE.SITENAME as SiteName,
ITEM.NAME as ItemName,
Sale.OBJID as SaleID,
SALEITEMS.QTY as Qty,
SALEITEMS.AMT as Amt
FROM SALE INNER JOIN SALEITEMS ON (SALE.OBJID = SALEITEMS.SALEID) AND (SALE.SITE = SALEITEMS.SITE)
INNER JOIN SITE ON (SALE.SITE = SITE.ID)
INNER JOIN ITEM ON (SALEITEMS.ITEM = ITEM.OBJID)
WHERE (SALE.LOGDATE BETWEEN (''''' + CONVERT(char(10), @BeginDate, 101) + ''''') AND (''''' + CONVERT(char(10), @EndDate, 101) + ''''') )
) as B
Where
A.Site = B.site and A.Saleid = B.saleid
'')
GROUP BY
SITENAME,
NAME,
ITEMNAME
'
Exec(@sql)
END