• Budd (5/6/2014)


    I have 2 identical tables one contains current settings, the other contains all historical settings.

    I could create a union view to display the current values from tableA and all historical values from tableB, but

    that would also require a Variable to hold the tblid for both select statements.

    Q. Can this be done with one joined or conditional select statement?

    DECLARE @tblid int = 501

    SELECT 1,2,3,4,'CurrentSetting'

    FROM TableA ta

    WHERE tblid = @tblid

    UNION

    SELECT 1,2,3,4,'PreviosSetting'

    FROM Tableb tb

    WHERE tblid = @tblid

    As David said providing ddl and sample data would be a big help.

    Why not just use a left join?

    DECLARE @tblid int = 501

    SELECT ta.[Columns], tb.[Columns]

    FROM TableA ta

    left join TableB tb on tb.tblid = ta.tblid

    where ta.tblid = @tblid

    Everything in ta is current and the values in tb are previous.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/