How to merge 2 resultset?

  • Hi guys,

     

    I have two resultset (or two tables), one with five records an the other one with the same number of records.

    What i want to do is to merge first row of first table with first row of the second table and so on.

    Is there a way to do this without cursors or any kink of loop?

     

    PinAnt

  • I hope you have a merge command in sql 2005 like what you have in oracle databases.

    Cheers,
    Sugeshkumar Rajendran
    SQL Server MVP
    http://sugeshkr.blogspot.com

  • merge command is still not available in SQL 2005. You have to

    1.Update destination table based on matching key vaules of first

    Update destination

    set col1=s.clo1, col2=s.col2

    from source s

    where s.key1=destination.key1

     

    2.Insert those records into destination table whose key values are not present in the source

     

    insert into destination (key1,col1,col2)

    select o.key1,s.clo1,s.clo2

    from (select key1 from source

          except

          select key1 from destination)o join source s

    on o.key1=s.key1

    if you find any better solution than this, please let me know []

      

     

      

    Regards
    Shrikant Kulkarni

  • PinAnt

    Could you post the structure and data your two original tables and what you want as a result.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • -- You can try this approach:

    select * from

    (

    select

    rn=row_number() over (order by column1),

    *

    from table1

    )t1

    join

    (

    select

    rn=row_number() over (order by column1),

    *

    from table2

    ) t2

    on t1.rn=t2.rn

  • Thanks!!!

    PinAnt

Viewing 6 posts - 1 through 5 (of 5 total)

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