Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have been trying to attempt this trigger but couldn't manage to do so. The program is supposed to turn the login attempts to 0 once the user is unblocked. Any help would be appreciated.

SQL
CREATE TRIGGER UserTrigger13
   ON  Users
   After update 
AS 
DECLARE @Username nvarchar(50)
DECLARE @Num int


SET @Username = (SELECT Username FROM updated)
SET @Num = 0

UPDATE Users SET UserLoginAttempts = @Num WHERE UserBlocked = 0 AND Users.Username = @Username

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here

END
GO
Posted
Updated 17-May-12 0:05am
v2

1 solution

Your begin..end block seems a bit odd. Should it be
SQL
CREATE TRIGGER UserTrigger13
   ON  Users
   After update 
AS 
  DECLARE @Username nvarchar(50);
  DECLARE @Num int;
BEGIN
  SET @Username = (SELECT Username FROM inserted);
  SET @Num = 0;
 
  UPDATE Users SET UserLoginAttempts = @Num 
  WHERE UserBlocked = 0 AND Users.Username = @Username;
END
GO


If the update statement isn't updating any rows, then the probable cause is in the conditions.

Check for example that the user blocked is really 0 when the trigger fire.

You can also use PRINT[^] to output the variables etc to get more information about the execution.
 
Share this answer
 
v2
Comments
kim061 17-May-12 6:33am    
I tried this and didn't work. I tried the tutorial and didn't find it to suit my needs
Wendelius 17-May-12 7:13am    
When you say it didn't work, what do you mean, did you get an error (if you did, what is it) or was no rows updated or something else?
kim061 17-May-12 7:14am    
I had a custom error made with a method and it stated that row cannot be updated
Wendelius 17-May-12 7:16am    
Ok, so what was the error you received from database. Pleas post also the code where the error occurred.
kim061 17-May-12 8:23am    
it wasn't an error just a predefined msg

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