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
 General SQL Server Forums
 New to SQL Server Programming
 Join problem

Author  Topic 

d4046949
Starting Member

9 Posts

Posted - 2008-08-13 : 10:11:58
Hey guys

I’m having some trouble getting some code to work.

I’ve written the following:


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[checkLogin]
-- Add the parameters for the stored procedure here
@login nvarchar(255),
@password nvarchar(255)

AS
BEGIN

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 User

END



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 stored
Table 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 = @userId

you mean @user right?


Em
Go to Top of Page

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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 10:18:53
quote:
Originally posted by d4046949

Hey guys

I’m having some trouble getting some code to work.

I’ve written the following:


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[checkLogin]
-- Add the parameters for the stored procedure here
@login nvarchar(255),
@password nvarchar(255)

AS
BEGIN

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 User

END



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 stored
Table 2 – where a little more info is stored + accountDisabled/enabled.

Any ideas…



you've declared variable as user and using it as userid
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 10:24:26
the above code is equivalent to

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[checkLogin]
-- Add the parameters for the stored procedure here
@login nvarchar(255),
@password nvarchar(255)

AS
BEGIN

SET NOCOUNT ON;

select isnull(nullif(member_deleted,1),-1)
from Table2 t2
join Table1 t1
on t1.member_id=t2.member_id
where t1.User_login = @login
and t1.User_pwd = @password

END
Go to Top of Page

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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 10:32:02
[code]set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[checkLogin]
-- Add the parameters for the stored procedure here
@login nvarchar(255),
@password nvarchar(255)

AS
BEGIN

SET NOCOUNT ON;

select isnull(nullif(member_deleted,1),-1)
from Table2 t2
join Table1 t1
on t1.member_id=t2.member_id
where t1.User_login = @login
and t1.User_pwd = @password
and EXISTS(SELECT 1 FROM User)

END[/code]
Go to Top of Page

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.
Go to Top of Page

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)

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-14 : 06:29:07
[code]set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[checkLogin]
-- Add the parameters for the stored procedure here
@login nvarchar(255),
@password nvarchar(255)

AS
BEGIN

SET NOCOUNT ON;

select CASE WHEN EXISTS(SELECT 1 FROM User)
THEN isnull(nullif(member_deleted,1),-1) ELSE 0 END
from Table2 t2
join Table1 t1
on t1.member_id=t2.member_id
where t1.User_login = @login
and t1.User_pwd = @password

END[/code]
Go to Top of Page

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.
Go to Top of Page

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 @table1
select 'test name','pass',1 union all
select 'a n other','pass2',2

declare @table2 table (member_id int, member_deleted bit)
insert into @table2
select 1,1 union all
select 2,0

declare @login varchar(50), @password varchar(50)

set @login = 'x'
set @password = 'c'
--set @login = 'test name'
--set @password = 'pass'


declare @result int

select @result= member_deleted*-1
from @Table1 t1
join @Table2 t2
on t1.member_id=t2.member_id
where t1.User_login = @login
and t1.User_pwd = @password

select coalesce(@result,-1)


Em
Go to Top of Page
   

- Advertisement -