sql server - INSERT and UPDATE after one row inserted MS SQL -
i don't know if i'm thinking correct, i'm open suggestions. i'm using ms sql server 2008 r2. here 'story' everytime insert row tbldelivered there trigger insert in tblconditiondel 5 values(1,1,1,1,1). table auto id increment. , inserted row must fieldname conditionid updated id tblconditiondel. think there wrong statement if delete statement id update entire table, must 1 inserted row.
my code:
alter trigger [dbo].[trgcond2] on [dbo].[tbldelivered] after insert insert tblconditiondel(con1,con2,con3,con4, con5) values(1,1,1,1,1); update tbldelivered set conditionid = (select max(conditionid) tblconditiondel) (select 1 inserted) = tbldelivered.conditionid thx in advance
your solution not work if more 1 row inserted.
do this
create table tbldelivered ( deliveredid int not null ,conditionid int ); create table tblconditiondel ( conditionid int identity(1,1) ,con1 int not null ,con2 int not null ,con3 int not null ,con4 int not null ,con5 int not null ); create trigger [dbo].[trgcond2] on [dbo].[tbldelivered] after insert set nocount on; declare @conditionids table ( -- stores inserted conditionsids deliveredid int not null ,conditionid int not null ); merge tblconditiondel -- insert not back upwards output multiple rows using inserted triggerinserted on 1 = 0 when not matched insert (con1, con2, con3, con4, con5) values (1,1,1,1,1) output triggerinserted.deliveredid ,inserted.conditionid @conditionids; update tbldelivered set conditionid = conditionids.conditionid tbldelivered inner bring together @conditionids conditionids on conditionids.deliveredid = tbldelivered.deliveredid -- test code insert tbldelivered (deliveredid) values (4),(5),(6); select * tblconditiondel select * tbldelivered sql-server triggers insert where
No comments:
Post a Comment