无法创建存储过程。在选择列表中只能指定一个表达式

时间:2016-11-22 09:55:43

标签: sql sql-server stored-procedures

我想创建一个存储过程,以根据用户登录凭据返回特定的整数值。发生以下错误:

  

当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。

这是我的疑问:

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

我该如何克服这个问题?

2 个答案:

答案 0 :(得分:5)

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

这不等于1,它将返回该行的所有字段。

将其更改为此...

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

答案 1 :(得分:0)

请尝试给你反馈

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