May 5, 2016 at 5:01 am
I would like to write a Select Statement, which will bring additional columns from different databases based on the value of Column.
Select
Site, Trans_num
case
When Site = 'Site1'
Select * from Site1DB.dbo.GetDetails where Site1DB.dbo.GetDetails.trans_num = GeneralTransactions.trans_num
When Site = 'Site2'
Select * from Site2DB.dbo.GetDetails where Site2DB.dbo.GetDetails.trans_num = GeneralTransactions.trans_num
When Site = 'Site3'
Select * from Site2DB.dbo.GetDetails where Site3DB.dbo.GetDetails.trans_num = GeneralTransactions.trans_num
end
From GeneralTransactions
May 5, 2016 at 5:08 am
skb 44459 (5/5/2016)
I would like to write a Select Statement, which will bring additional columns from different databases based on the value of Column.Select
Site, Trans_num
case
When Site = 'Site1'
Select * from Site1DB.dbo.GetDetails where Site1DB.dbo.GetDetails.trans_num = GeneralTransactions.trans_num
When Site = 'Site2'
Select * from Site2DB.dbo.GetDetails where Site2DB.dbo.GetDetails.trans_num = GeneralTransactions.trans_num
When Site = 'Site3'
Select * from Site2DB.dbo.GetDetails where Site3DB.dbo.GetDetails.trans_num = GeneralTransactions.trans_num
end
From GeneralTransactions
You can't do it quite like that.
Instead, create a variable to hold site and populate it. Then
if @Site = 'Site1'
begin
select ... ;
end
if @Site = 'Site2'
begin
select ... ;
end
...
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
May 5, 2016 at 5:21 am
I don't know if I can use variables in select. I want to create a view for my select statement
May 5, 2016 at 5:27 am
you probably need to simply join three groups of data together to create your view; it might be slow;
CREATE VIEW vwGeneralTransactionsMultiSite
AS
SELECT T1.Site,
T1.Trans_num,
T2.*
FROM GeneralTransactions
INNER JOIN Site1DB.dbo.GetDetails T2
ON T1.trans_num = T2.trans_num
AND T1.Site = 'Site1'
UNION ALL
SELECT T1.Site,
T1.Trans_num,
T2.*
FROM GeneralTransactions
INNER JOIN Site2DB.dbo.GetDetails T2
ON T1.trans_num = T2.trans_num
AND T1.Site = 'Site2'
UNION ALL
SELECT T1.Site,
T1.Trans_num,
T2.*
FROM GeneralTransactions
INNER JOIN Site3DB.dbo.GetDetails T2
ON T1.trans_num = T2.trans_num
AND T1.Site = 'Site3'
Lowell
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply