Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new to sql server . i am having problem to add a trigger to delete a record when the value of quantity column reaches zero .i am working with netbeans and sql server.

What I have tried:

i am new so i haven't tried anything yet.
Posted
Updated 14-Jul-16 20:40pm
Comments
Herman<T>.Instance 15-Jul-16 2:32am    
Look HERE. Comes with explanation and examples.

1 solution

Hello,

Here is the solution to remove the rows from table when row count is reached to 12. this demo sample where we are maintaining users password in history (most recent 12 passwords) table for each users.
SQL
CREATE TRIGGER [dbo].[trgChangePassword] ON [dbo].[UserPassword] 
FOR INSERT
AS
	DECLARE @userid INT
	DECLARE @totalrec INT
	SELECT @userid = UserId FROM inserted
	SELECT @totalrec=COUNT(*) FROM UserPassword where UserId=@userid;
	IF @totalrec >= 12
	BEGIN 
		DELETE FROM UserPassword WHERE Id IN (SELECT TOP 1 Id FROM UserPassword WHERE UserId=@userid ORDER BY id ASC)
	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