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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -