sql server - How can I update the data in a base SQL table based on data in another table? -
i have table use store data. table populated when user clicks save on grid on client system. here made simple example. table called tablea. client screen shows 5 rows , when user clicks save translated 5 inserts:
create table [dbo].[tablea] ( [ida] int identity (1, 1) not null, [valuea] char(10) not null ) insert tablea values (1, 'one') insert tablea values (2, 'two') insert tablea values (3, 'three') insert tablea values (4, 'four') insert tablea values (5, 'five') go
now user on client changes data in grid , read latest data temp table. here simulate tableb
create table [dbo].[tableb] ( [idb] int identity (1, 1) not null, [valueb] char(10) not null ) insert tableb values (1, 'one') insert tableb values (3, 'newthree') insert tableb values (4, 'newfour') insert tableb values (5, 'five') go
can suggest how can use new data in tableb update rows in tablea. need in case delete row id of 2 , update rows ids of 4 , 5. each time different. example user may add rows , need add new row tablea. note happens inside stored procedure. need advice on different ways join tablea , tableb somehow create insert, delete , update tablea latest state.
note particular example need end is:
an update join:
update tablea set tablea.valueb = tableb.valua tablea join tableb on tablea.idb = tableb.ida
then delete extraneous rows:
delete tablea not exists (select 1 tableb tableb.ida = tablea.idb)
Comments
Post a Comment