performance - SQL Handling 2 tables efficiently by Joins -


i have described scenarios have efficient 1 sql query. have tried using temporary tables, i'm finding myself no where. seeking expert opinions. i'm using sql server 2012.

i have 2 tables table , table b.

    table     id  testname          table b     id  tableaid lastupdate  

tableaid in table b id column of table a.

scenario 1 :

    table     id  testname         1   test 1            table b     id  tableaid lastupdate  

if table b not have records, insert table records table b

scenario 2:

    table     id  testname         1   test 1            table b     id  tableaid lastupdate     1   1  

if table has records , matches records in table b, donot action

scenario 3:

    table     id  testname      1   test 1       2   test 2            table b     id  tableaid lastupdate     1   1  

if table has records , not matching in table b, insert table b

scenario 4:

    table     id  testname       table b     id  tableaid lastupdate     1   1     2   2  

if table has no records, update current date in lastupdate in table b records

i' planned have 2 temprory tables , use except filter matching records. i'm finding difficult write efficient query.

this seems perfect excuse use merge command

merge tableb b using tablea on a.id = b.tableaid when not matched target     insert (tableaid) values (a.id) when not matched source     update set lastupdate = getdate(); 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -