Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update a field

Author  Topic 

Dinghus
Starting Member

4 Posts

Posted - 2009-03-30 : 16:52:36
This one should be so easy but I am obviously missing something.
Basically, when a person submits their login info to a website, the stored procedure is called to 1) verify their login 2) update a "logins" counter in the table 3) they are only allowed 3 logins
This is for downloading software they have bought.

Everything works great EXCEPT the increment.

ALTER PROCEDURE CheckLogIn
@email varchar(50),
@password varchar(50),
@validate bit OUTPUT
AS
BEGIN
DECLARE @verified varchar;
SET @verified = (SELECT transactionID
FROM Purchases
WHERE email=@email AND pword=@password);
IF @verified IS NULL
set @validate = 0
ELSE
DECLARE @counter int;
SET @counter = (select count(transactionID) FROM purchases WHERE transactionid = @verified);
IF @counter >= 3
set @validate = 0
ELSE
BEGIN
SET @counter = @counter + 1;
UPDATE Purchases
SET logins = @counter
WHERE transactionid = @verified;
SET @validate = 1
END
END
GO

That is my latest attempt by pulling the count of logins out and then incrementing it and then trying to update the table.
Help me before my forehead is bloody from beating my head against my monitor.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-30 : 17:00:56
When you say it's not working, could you elaborate on what you mean since we aren't familiar with your environment? Are you getting an error? Is it updating the table with the wrong value? Is @counter receiving the correct value? ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Dinghus
Starting Member

4 Posts

Posted - 2009-03-30 : 17:42:37
The value in the logins field is not updating.
I primarily work in Oracle, so I'm thinking I'm not implementing this correctly. The default value is 0 since I don't want a null in there.
I've tried the update with logins=logins+1 and that didn't work either. I know it is getting inside that loop because I get a return of 1 (meaning successful login).
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-30 : 18:26:29
You say the default value is 0, however that isn't true as that is not part of your code. After you declare @counter, set it to 0 with SET @counter = 0. By default, it is NULL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Dinghus
Starting Member

4 Posts

Posted - 2009-03-30 : 18:31:39
Should have clarified that the field "logins" is default to 0 when the file is created. But that is a good point about setting @counter to 0. Still doesn't explain why doing an increment (logins = logins + 1) doesn't work. But I'll see if this helps.
Go to Top of Page

Dinghus
Starting Member

4 Posts

Posted - 2009-03-30 : 18:47:37
That didn't work. Of course I had already set @counter in the line with the query. So it would not be null unless nothing is returned. When I use a legitimate login, it still does not increment.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-30 : 22:06:00
Did you try adding a PRINT statement to the code to see if @counter is set properly? PRINT @counter

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -