sql - How to write a Stored Procedure to Select, Delete and then Insert? -
i loading sample data ui testing , trying check if data exists. if delete , insert new 1 instead. got 9 inserts , not sure if need check each row if exists delete each row insert.
this sample data trying load using sp.
insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp', getdate(), getdate(), 1); insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp1', getdate(), getdate(), 1); insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp2', getdate(), getdate(), 2); insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp3', getdate(), getdate(), 3); insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp4', getdate(), getdate(), 4); insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp5', getdate(), getdate(), 5); ; insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp6', getdate(), getdate(), 6); insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp7', getdate(), getdate(), 7); insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (212019, 'source', 'comp8', getdate(), getdate(), 8);
stored procedure
create procedure transaction @transaction_id int, @mdata_attrb varchar, @mdata_value varchar, @isactive bit begin if exists (select * trans_mdata transaction_id = @transaction_id) begin delete trans_mdata transaction_id = @transaction_id end else insert trans_mdata (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) values (@transaction_id, mdata_attrb, mdata_value, getdate(), getdate(), @isactive) end end exec [transaction] @transaction_id = 123456, @mdata_attrb = 'source', @mdata_value = 'backend', @isactive bit = 1
you can use except in insert check data matching across many columns:
-- set cte pseudo-table of test data: ; testvalues ( select distinct transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive ( values (212019, 'source', 'comp', getdate(), getdate(), 1) , (212019, 'source', 'comp1', getdate(), getdate(), 1) ,(212019, 'source', 'comp2', getdate(), getdate(), 2) ,(212019, 'source', 'comp3', getdate(), getdate(), 3) ,(212019, 'source', 'comp4', getdate(), getdate(), 4) ,(212019, 'source', 'comp5', getdate(), getdate(), 5) ,(212019, 'source', 'comp6', getdate(), getdate(), 6) ,(212019, 'source', 'comp7', getdate(), getdate(), 7) ,(212019, 'source', 'comp8', getdate(), getdate(), 8) ) t1 (transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) ) -- insert not in target table (except): insert trans_mdata(transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive) select transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive testvalues except select transaction_id, mdata_attrb, mdata_value, created_time, last_mod_time, isactive trans_mdata
Comments
Post a Comment