| Author |
Topic |
|
d4046949
Starting Member
9 Posts |
Posted - 2008-08-13 : 10:11:58
|
Hey guysI’m having some trouble getting some code to work.I’ve written the following:set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[checkLogin] -- Add the parameters for the stored procedure here @login nvarchar(255), @password nvarchar(255)ASBEGIN SET NOCOUNT ON; declare @user int; declare @exists int; SELECT @user = member_id from Table1 where User_login = @login and User_pwd = @password select @exists = member_deleted from Table2 where member_id = @userId if @exists = 1 return -1 else select top 1 isnull(@exists,-1) from UserEND Basically its meant to do two things: If a user enters a nonexistent username and password then the system returns -1. However if that account has been deleted – ie a Boolean flag set to true – it should also return -1. The problem is that this is in a different table.Table 1 – where username and password are storedTable 2 – where a little more info is stored + accountDisabled/enabled.Any ideas… |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-08-13 : 10:14:04
|
| select @exists = member_deleted from Table2 where member_id = @userIdyou mean @user right?Em |
 |
|
|
d4046949
Starting Member
9 Posts |
Posted - 2008-08-13 : 10:17:47
|
| ooops my bad, yes I do mean @user i just didnt change it when i copied it in here. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-13 : 10:18:53
|
quote: Originally posted by d4046949 Hey guysI’m having some trouble getting some code to work.I’ve written the following:set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[checkLogin] -- Add the parameters for the stored procedure here @login nvarchar(255), @password nvarchar(255)ASBEGIN SET NOCOUNT ON; declare @user int; declare @exists int; SELECT @user = member_id from Table1 where User_login = @login and User_pwd = @password select @exists = member_deleted from Table2 where member_id = @userId if @exists = 1 return -1 else select top 1 isnull(@exists,-1) from UserEND Basically its meant to do two things: If a user enters a nonexistent username and password then the system returns -1. However if that account has been deleted – ie a Boolean flag set to true – it should also return -1. The problem is that this is in a different table.Table 1 – where username and password are storedTable 2 – where a little more info is stored + accountDisabled/enabled.Any ideas…
you've declared variable as user and using it as userid |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-13 : 10:24:26
|
the above code is equivalent toset ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[checkLogin] -- Add the parameters for the stored procedure here @login nvarchar(255), @password nvarchar(255)ASBEGIN SET NOCOUNT ON; select isnull(nullif(member_deleted,1),-1) from Table2 t2join Table1 t1on t1.member_id=t2.member_id where t1.User_login = @login and t1.User_pwd = @password END |
 |
|
|
d4046949
Starting Member
9 Posts |
Posted - 2008-08-13 : 10:29:23
|
| Thanks for the reply.however if i put an nonexistant user in i get the returned value of 0 and same if i put a user in thats been disabled. I should get -1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-13 : 10:32:02
|
| [code]set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[checkLogin] -- Add the parameters for the stored procedure here @login nvarchar(255), @password nvarchar(255)ASBEGIN SET NOCOUNT ON; select isnull(nullif(member_deleted,1),-1) from Table2 t2join Table1 t1on t1.member_id=t2.member_id where t1.User_login = @login and t1.User_pwd = @passwordand EXISTS(SELECT 1 FROM User) END[/code] |
 |
|
|
d4046949
Starting Member
9 Posts |
Posted - 2008-08-13 : 10:40:06
|
| hmm, still returns 0 rather than -1 if it doesnt exist, or disabled. |
 |
|
|
d4046949
Starting Member
9 Posts |
Posted - 2008-08-14 : 05:14:34
|
| I've played around with the code a little more, but it still seems to return 0. Now I wonder if its to do with this statement failing every time:select isnull(nullif(member_deleted,1),-1) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-14 : 06:29:07
|
| [code]set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[checkLogin] -- Add the parameters for the stored procedure here @login nvarchar(255), @password nvarchar(255)ASBEGIN SET NOCOUNT ON; select CASE WHEN EXISTS(SELECT 1 FROM User)THEN isnull(nullif(member_deleted,1),-1) ELSE 0 ENDfrom Table2 t2join Table1 t1on t1.member_id=t2.member_id where t1.User_login = @login and t1.User_pwd = @password END[/code] |
 |
|
|
d4046949
Starting Member
9 Posts |
Posted - 2008-08-14 : 09:09:43
|
| Hmm something must be wrong somewhere, cause no matter which code version I use it still returns 0 rather than correct values. |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-08-14 : 09:20:03
|
declare @table1 table (user_login varchar(50),user_pwd varchar(50),member_id int)insert into @table1select 'test name','pass',1 union allselect 'a n other','pass2',2declare @table2 table (member_id int, member_deleted bit)insert into @table2select 1,1 union allselect 2,0declare @login varchar(50), @password varchar(50)set @login = 'x'set @password = 'c'--set @login = 'test name'--set @password = 'pass'declare @result intselect @result= member_deleted*-1 from @Table1 t1join @Table2 t2on t1.member_id=t2.member_id where t1.User_login = @login and t1.User_pwd = @passwordselect coalesce(@result,-1) Em |
 |
|
|
|