Discussion:
Using @@Rowcount
(too old to reply)
Bob
2012-05-01 18:29:42 UTC
Permalink
Hello.

I'm writing a s proc to authenticate users.

So I want to see if there is 1 record "....where UserID='somevalue'
and Password='someothervalue'

if the @@rowcount is 1 then i want to return a 1 from the sp,
otherwise I want to return a 0

Can you help?

Bob
Jeroen Mostert
2012-05-01 18:36:31 UTC
Permalink
Post by Bob
I'm writing a s proc to authenticate users.
So I want to see if there is 1 record "....where UserID='somevalue'
and Password='someothervalue'
otherwise I want to return a 0
Can you help?
Don't use @@ROWCOUNT if it can be helped, it's more volatile than ice on a
hot summer day.

Use COUNT(*) if you literally want to return only 1 if there is precisely
one row, otherwise EXISTS:

IF (SELECT COUNT(*) FROM Users WHERE UserID = @UserID AND Password =
@Password) = 1 RETURN 1 ELSE RETURN 0;

IF EXISTS (SELECT * FROM Users WHERE UserID = @UserID AND Password =
@Password) RETURN 1 ELSE RETURN 0;

The usual caveats apply to things involving passwords: you're not supposed
to store them in plaintext but to store salted hashes, and if at all
possible don't muck with homegrown passwords at all but re-use an existing
authentication scheme.
--
J.
Bob
2012-05-01 18:41:01 UTC
Permalink
On 2012-05-01 20:29, Bob wrote:> I'm writing a s proc to authenticate users.
Post by Bob
So I want to see if there is 1 record "....where UserID='somevalue'
and Password='someothervalue'
otherwise I want to return a 0
Can you help?
hot summer day.
Use COUNT(*) if you literally want to return only 1 if there is precisely
@Password) = 1 RETURN 1 ELSE RETURN 0;
@Password) RETURN 1 ELSE RETURN 0;
The usual caveats apply to things involving passwords: you're not supposed
to store them in plaintext but to store salted hashes, and if at all
possible don't muck with homegrown passwords at all but re-use an existing
authentication scheme.
--
J.
Thanks J.

Continue reading on narkive:
Loading...