【问题标题】:Incorrect syntax near the keyword else in stored procedure存储过程中关键字 else 附近的语法不正确
【发布时间】:2017-09-23 15:37:05
【问题描述】:
create proc login
    (@id int out,
     @email varchar(30),
     @passsword varchar(30),
     @type varchar(30) out)
as
begin
    select 
        @id = id, @type = type 
    from 
        registration 
    where 
        email = @email 
        and password = @password

    return 0
end
else
begin
    return 1
end

【问题讨论】:

  • 这个程序应该做什么?
  • 我认为你需要if - else 在这里没有意义。请先做一些阅读 - 你甚至没有在这里提问。
  • 它是一个登录程序,你可以用 if 编辑它吗,我几乎没有尝试过
  • 始终加密密码。 从不存储明文密码!

标签: sql procedure


【解决方案1】:

大概,你正在寻找这个:

create procedure usp_login (
    @email varchar(30),
    @passsword varchar(30),
    @id int output,
    @type varchar(30) output
) as
begin
    set @id = NULL;

    select @id = id, @type = type
    from registration
    where email = @email and password = @password;

    return (case when @id is null then 0 else 1 end);
end;

一些注意事项:

  • 您的代码表明您使用的是未加密的密码。这是您需要学习如何解决的第一件事。
  • else -- 如 cmets 中所述 -- 需要 if
  • 当您混合输入和输出参数时,我更喜欢将output 参数设置为最后一个参数。这是一个偏好问题。不过,我认为将所有输入和所有输出放在一起是一种很好的做法——除非你有充分的理由。
  • 用分号结束语句。
  • 使用存储过程的返回值实际上是一种最佳实践。但是,这不是必需的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 2014-06-22
    • 2020-07-05
    • 2016-06-08
    • 1970-01-01
    • 2013-12-16
    • 2020-06-23
    • 2017-11-22
    相关资源
    最近更新 更多