Searching in two tables and add final result to a string

  • Hi Team,

    I got exact results from Team for whatever i've posted,

    Very Very thanks.

    Here my requirement is :

    I have two tables,

    table 1 : Test1

    ColA|Value

    ----------------------

    A100

    C300

    Table 2 : Test2

    ColA|Value

    -----------------------

    B200

    D150

    if Record "A" is available in table1 then add string to a variable

    @string = A : 300

    else search in the table 2

    Next

    search for B, it is not available in table 1, then get from table 2

    @ string = A : 300, B 200

    am using below query for searching in table 1, how to include table 2 for searching.

    SELECT RTRIM(

    + 'ColA:'+ISNULL(ColA,'')+', '

    + ISNULL('ColB:'+CAST(ColB AS VARCHAR(10))+' ','')

    + ISNULL('ColC:'+CAST(ColC AS VARCHAR(10))+' ','')

    + ISNULL('ColD:'+CAST(ColD AS VARCHAR(10))+' ','')

    )

    FROM

    Please help.........

  • Minnu (12/4/2012)


    am using below query for searching in table 1, how to include table 2 for searching.

    you need to make JOIN with second table with first one.

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Yes, Using joins..

    First it need to check in table1, if value found then assign that values to a string,

    if not found then only it should check in table2.

    table 1 : Test1

    ColA|Value

    ----------------------

    A100

    Table 2 : Test2

    ColA|Value

    -----------------------

    B200

    if COLA =A is available in Table1 then

    COLA : A Value: 100

    else

    check in Table2, if available

    COLA : A Value: 100

    Query Please...

  • Please stop cross posting. You don't need to start a new thread for each follow up question.

    http://www.sqlservercentral.com/Forums/Topic1392324-391-1.aspx

    _______________________________________________________________

    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/

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

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