0

I want to create a stored procedure to return specific integer values depending on user login credentials. The following error occurs:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

This is my query:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE Login 
    -- Add the parameters for the stored procedure here
    @userName nvarchar(100),
    @password nvarchar(100)
AS
BEGIN

    SET NOCOUNT ON;

   IF((Select COUNT(*) as Count FROM SimpleSite.dbo.Users WHERE UserName = @userName)=1)
      BEGIN
       IF((SELECT * FROM SimpleSite.dbo.Users WHERE UserName = @userName AND [Password] = @password)=1)
       -- valid login 
       RETURN 1
       ELSE       
       -- wrong password      
       RETURN 2
      END           
   ELSE
       -- No account associated with the username 
       RETURN 3          
END
GO

How can I overcome this problem ?

Bugs
  • 4,491
  • 9
  • 32
  • 41
Nilaksha Perera
  • 715
  • 2
  • 12
  • 36

2 Answers2

5
   IF((SELECT * FROM SimpleSite.dbo.Users WHERE UserName = @userName AND [Password] = @password)=1)

This will not equal 1, it will return all the fields for the row.

Change it to this...

   IF((SELECT count(*) FROM SimpleSite.dbo.Users WHERE UserName = @userName AND [Password] = @password)=1)
ChrisBint
  • 12,773
  • 6
  • 40
  • 62
0

Please try and give you feedback

IF EXISTS((Select COUNT(1) as Count FROM SimpleSite.dbo.Users WHERE UserName = @userName AND [Password] = @password)=1)
      BEGIN
       IF((SELECT COUNT(1) FROM SimpleSite.dbo.Users WHERE UserName = @userName AND [Password] = @password)=1)
       -- valid login 
       RETURN 1
       ELSE       
       -- wrong password      
       RETURN 2
      END           
   ELSE
       -- No account associated with the username 
       RETURN 3  
A.Goutam
  • 3,422
  • 9
  • 42
  • 90