如何在返回值附近修复错误的语法

时间:2019-02-11 09:06:38

标签: sql sql-server

我正在尝试从员工表中获取员工等级并设置值 基于等级并返回整数值,但由于返回附近的语法错误,我在第二行得到错误 我正在尝试学习sql server

 create procedure checkgrade(@empid int)
    returns int
    as 
    begin
        declare @cgrade char(1)
        declare @rint int
            select @cgrade=grade from employee where @empid=empid
            if @cgrade='M' set @rint =1
            else if  @cgrade='L' set @rint=2
            else if @cgrade='T' set @rint=3
            else if @cgrade='X' set @rint= 4
            else set @rint=0
        return @rint
    end;

4 个答案:

答案 0 :(得分:3)

您已在过程中使用函数的语法。

create Function checkgrade(@empid int)
returns int
as 
begin
    declare @cgrade char(1)
    declare @rint int
        select @cgrade=grade from employee where @empid=empid
        if @cgrade='M' set @rint =1
        else if  @cgrade='L' set @rint=2
        else if @cgrade='T' set @rint=3
        else if @cgrade='X' set @rint= 4
        else set @rint=0

    return @rint
end;

答案 1 :(得分:1)

您可以使用Function代替Procedure

create Function checkgrade(@empid int)
    returns int
    as 
    begin
        declare @cgrade char(1)
        declare @rint int
            select @cgrade=grade from employee where @empid=empid
            if @cgrade='M' set @rint =1
            else if  @cgrade='L' set @rint=2
            else if @cgrade='T' set @rint=3
            else if @cgrade='X' set @rint= 4
            else set @rint=0
        return @rint
    end;

答案 2 :(得分:0)

您可以使用output参数输出结果

create procedure checkgrade @empid int, @rint int OUTPUT   
    as 
    begin
        declare @cgrade char(1)

            select @cgrade=grade from employee where @empid=empid
            if @cgrade='M' set @rint =1
            else if  @cgrade='L' set @rint=2
            else if @cgrade='T' set @rint=3
            else if @cgrade='X' set @rint= 4
            else set @rint=0       
    end

答案 3 :(得分:0)

CREATE PROCEDURE的语法略有不同:

 create procedure checkgrade(@empid int)
    as
    begin
        declare @cgrade char(1)
        declare @rint int
            select @cgrade=grade from employee where @empid=empid
            if @cgrade='M' set @rint =1
            else if  @cgrade='L' set @rint=2
            else if @cgrade='T' set @rint=3
            else if @cgrade='X' set @rint= 4
            else set @rint=0
        return @rint
    end;

实际上,可能是您需要功能而不是其他成员建议的过程