Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
3.86/5 (3 votes)
I have a table which is something like this

SQL
create table shop.stock(
shopid int foreign key references shop.name(shopid)
itemid int foreign key references item.item(itemid)
stockremaining int)


where shop.name contains all the details of the shop, item.item contains all the details of the item. I want to create an on update trigger which will book the items if the stocks available in a particular store for a particular item is less than 10
I use a code something like
SQL
create trigger [shop].[trigonupdate] on [shop].[stock]
after
update
as
declare @stockremaining int;
declare @itemid int;
declare @shopid int;


if(@stockremaining<=10)
begin
insert into booking.items values(@itemid,@shopid,10);
end

problem is how do i get to know which itemid and which shopid are updated?
The above trigger will not work as all the variables are assigned to null values
Posted
Comments
ZurdoDev 4-Oct-13 7:28am    
You use the inserted and deleted tables. They are accessible in triggers.
Corporal Agarn 4-Oct-13 7:37am    
@stockremaining will not have a value unless it is in the table 'inserted' or 'deleted'. (or can be calculated from)

1 solution

I have solved this myself
i created a table called booking like this
SQL
create table booking(
bookingid int identity(1,1) primary key,
itemid int foreign key references item.item(itemid),
shopid int foreign key references shop(shopid),
quantity int
);

now i create the trigger for insert as
SQL
create  trigger [shop].[trigoninsert] on [shop].[stock]
for 
insert
as
declare @stockremaining int;
declare @itemid int;
declare @shopid int;
select @stockremaining=stockremaining from inserted;
select @itemid=itemid from inserted;
select @shopid=shopid from inserted;

 

if(@stockremaining<=10)
begin
insert into booking values(@itemid,@shopid,10);
end 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900