Help with select statement when WHEN Clause involving multiple databases

  • 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

  • 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

    ...

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I don't know if I can use variables in select. I want to create a view for my select statement

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply