Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My update trigger should fire only for values greater than 50000 if i change the values lesser than 50000 it should not get fired in bigstores but it is getting fired
SQL
IF EXISTS ( SELECT Name FROM sysobjects 
            WHERE Name = 'TRI_deleteINSTOCK' AND Type = 'P' )
DROP trigger TRI_updateINSTOCK
GO
CREATE TRIGGER TRI_updateINSTOCK
ON instock
FOR update
AS
BEGIN
	DECLARE 
		@storeid  smallint, 
		@totalquantity int

	SELECT @storeid= i.storeid FROM inserted i
	
	select @totalquantity =  SUM(QUANTITY)
					         FROM instock
					         WHERE storeid = @storeid

	IF NOT EXISTS(SELECT storeid FROM bigstores WHERE storeid =@storeid  )
	BEGIN
	
		insert into  bigstores
		select * from stores
		where StoreId = @StoreId AND @totalquantity >= 50000 
	END

END
GO
Posted
Updated 27-Jan-15 19:20pm
v2

Check out below solution, this might help you to understand how you will achieve this.
http://stackoverflow.com/questions/280904/trigger-to-fire-only-if-a-condition-is-met-in-sql-server[^]
 
Share this answer
 
XML
First of all your IF EXISTS need corrections
<pre lang="SQL">
IF EXISTS ( SELECT Name FROM sysobjects
WHERE Name = 'TRI_deleteINSTOCK' AND Type = 'P' )
DROP trigger TRI_updateINSTOCK
GO
</pre>

it should be
<pre lang="SQL">
IF EXISTS ( SELECT Name FROM sysobjects
WHERE Name = 'TRI_updateINSTOCK' AND Type = 'P' )
DROP trigger TRI_updateINSTOCK
GO
</pre>

Trigger is working fine as defined there might be an understanding issue which is the following:

Let say Store A quantity exceed 50,000. this will be inserted in big store table.

what if someone check the records in the instock table and think the quantity entered is wrong and he/she edit the quantity, doing so the total quantity goes less then 50,000.

now where that store should be a big store or not ?
 
Share this answer
 
Comments
Member 11387088 28-Jan-15 1:38am    
If one edits the value lesser than 50000 means it should not get fired in big stores but in my query it is getting fired what to do now
Umer Akram 28-Jan-15 2:01am    
you are saying that a store which was not a in bigstore table. it value updated and its total quantity is less then 50,000 and still ends up in the bigstore.

that highly unlikely, because that store might be a in bigstore table 1st.


for your understanding check the following example:

USE tempdb
GO

create table instock ( storeid int, QUANTITY int)
create table bigstores ( storeid int)
create table stores ( storeid int)
GO
CREATE TRIGGER TRI_updateINSTOCK
ON instock
FOR update
AS
BEGIN
DECLARE
@storeid smallint,
@totalquantity int

SELECT @storeid= i.storeid FROM inserted i

select @totalquantity = SUM(QUANTITY)
FROM instock
WHERE storeid = @storeid

IF NOT EXISTS(SELECT storeid FROM bigstores WHERE storeid =@storeid )
BEGIN

select @totalquantity

insert into bigstores
select * from stores
where StoreId = @StoreId AND @totalquantity >= 50000
END

END
GO

insert into stores
select 1 union all
select 2 union all
select 3 union all
select 4

insert into instock
select 1, 10000
from stores

---------------------- updated the quantity added to exceed 50,000
update instock
set QUANTITY = QUANTITY + 10000
where storeid = 1

---------------------- updated the quantity drop from 50,000
update instock
set QUANTITY = QUANTITY - 10000
where storeid = 1

GO

drop table instock
drop table bigstores
drop table stores

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