December 5, 2016 at 12:08 pm
I have a Table which have two columns 1. UserName 2.Password.
Now I need to create stored procedure with two parameters 1.@username nvarchar(255) 2. Password nvarchar(255)
When I execute the stored procedures. If userId and password match then it should print login successful and if it doesnot match it should print login unsucessfull.
Please help me out.
Thanks in advance.
December 5, 2016 at 12:12 pm
That's pretty basic. What have you tried? What problem are you facing?
If this isn't homework, I'd be really concerned if you're handling passwords on plain text.
December 5, 2016 at 12:16 pm
create procedure spgetuserdetails
(
@userid nvarchar(255),
@Password nvarchar(255)
)
as
begin
declare @count int
select @count = count(*) from UsersTable where UserId = @userid and [Password] = @Password
if(@count = 0)
begin
Print 'Not Successful'
end
else
begin
print 'Login successfull'
end
end
I tried this. Is this right?
December 5, 2016 at 12:22 pm
swarun999 (12/5/2016)
create procedure spgetuserdetails(
@userid nvarchar(255),
@Password nvarchar(255)
)
as
begin
declare @count int
select @count = count(*) from UsersTable where UserId = @userid and [Password] = @Password
if(@count = 0)
begin
Print 'Not Successful'
end
else
begin
print 'Login successfull'
end
end
I tried this. Is this right?
First note the comments about storing passwords in plain text. Don't. So I am assuming that this is not a real-world example.
Second, successful has only one 'l'.
Third, a more efficient way of achieving your logic uses EXISTS:
IF NOT EXISTS
(
SELECT 1
FROM dbo.UsersTable
WHERE
UserId = @userid AND
Password = @Password
)
BEGIN
PRINT 'Not Successful';
END;
ELSE
BEGIN
PRINT 'Login Successful';
END;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
December 5, 2016 at 12:29 pm
swarun999 (12/5/2016)
create procedure spgetuserdetails(
@userid nvarchar(255),
@Password nvarchar(255)
)
as
begin
declare @count int
select @count = count(*) from UsersTable where UserId = @userid and [Password] = @Password
if(@count = 0)
begin
Print 'Not Successful'
end
else
begin
print 'Login successfull'
end
end
I tried this. Is this right?
You tried it or you wrote it? If you try it, you'll find out if it's right or not.
I suggest that you try Phil's option. You won't need a variable and the query is less expensive.
Remember, ALWAYS test.
December 5, 2016 at 12:37 pm
Yes, I will do. Thanks for your help
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply