SQL Server 2008: updating and keeping tracking of anuual price increase -
i have project right need update prices list of products. prices 1/1/2007 12/31/2011 given , need increase prices 5% each year, until end of 2015.
here have. getting stuck on updating prices (ie 5% increase). keep getting error message duplicate data. in advance help/hints !
error message:
msg 2627, level 14, state 1, procedure update_history, line 9
violation of primary key constraint 'pk_pricecha_207f7de23a81b327'. cannot insert duplicate key in object 'dbo.pricechange_history'.
tables:
create table pricechange (productid integer not null primary key, startdate date, endingdate date, unitprice money); alter table pricechange add foreign key (productid) references product(productid) create table pricechange_history (history_productid integer not null primary key, history_startdate date, history_endingdate date, history_unitprice money, modified_date datetime, changetype varchar(20) ); alter table pricechange_history add foreign key (history_productid) references product(productid)
triggers
create trigger [insert_history] on pricechange insert insert pricechange_history (history_productid, history_startdate, history_endingdate, history_unitprice, modified_date, changetype) select productid, startdate, endingdate, unitprice, getdate(), 'inserted' inserted create trigger [update_history] on pricechange update insert pricechange_history(history_productid, history_startdate, history_endingdate, history_unitprice, modified_date, changetype) select productid, startdate, endingdate, unitprice, getdate(), 'before update' deleted insert pricechange_history(history_productid, history_startdate, history_endingdate, history_unitprice, modified_date, changetype) select productid, startdate, endingdate, unitprice, getdate(), 'after update' inserted
insert + update
insert pricechange values(1,'1/1/2007', '12/31/2011', 500) <--- record query both pricechange , pricechange history update pricechange set unitprice = (unitprice * 1.05), startdate = '1/1/2012', endingdate = '12/31/2012' productid = 1 update pricechange set unitprice = (unitprice * 1.05), startdate = '1/1/2013', endingdate = '12/31/2013' productid = 1 update pricechange set unitprice = (unitprice * 1.05), startdate = '1/1/2014', endingdate = '12/31/2014' productid = 1 update pricechange set unitprice = (unitprice * 1.05), startdate = '1/1/2015', endingdate = '12/31/2015' productid = 1
price_history has pk on productid cannot have duplicate products. add identity primary key.
Comments
Post a Comment