SQL:我只得到一行作为输出,我应该得到4.我的代码出了什么问题

时间:2016-03-30 20:46:09

标签: sql-server stored-procedures

这是我正在使用的代码

CREATE PROCEDURE Sp_bonus @bonus      INT, 
                          @lastname   VARCHAR(50) out, 
                          @firstname  VARCHAR(50) out, 
                          @dateofhire DATE out, 
                          @salary     SMALLMONEY out, 
                          @bonusmoney SMALLMONEY out 
AS 
    IF @bonus = 20 
      BEGIN 
          SELECT @lastname = lastname, 
                 @firstname = firstname, 
                 @dateofhire = dateofhire, 
                 @salary = salary, 
                 @bonusmoney = ( salary * ( @bonus ) * 0.01 ) 
          FROM   salesreps 
          WHERE  dateofhire < '12/31/2000' 
      END 

    IF @bonus = 5 
      BEGIN 
          SELECT @lastname = lastname, 
                 @firstname = firstname, 
                 @dateofhire = dateofhire, 
                 @salary = salary, 
                 @bonusmoney = ( salary * ( @bonus ) * 0.01 ) 
          FROM   salesreps 
          WHERE  dateofhire >= '12/31/2000' 
      END 

    DECLARE @lastname   VARCHAR(50), 
            @firstname  VARCHAR(50), 
            @dateofhire DATE, 
            @salary     SMALLMONEY, 
            @bonusmoney SMALLMONEY 

    EXEC Sp_bonus 
      @bonus=20, 
      @lastname=@lastname out, 
      @firstname=@firstname out, 
      @dateofhire=@dateofhire out, 
      @salary=@salary out, 
      @bonusmoney=@bonusmoney out 

    SELECT lastname=@lastname, 
           firstname=@firstname, 
           dateofhire=@dateofhire, 
           salary=@salary, 
           bonus=@bonusmoney 

结果是

 Lastname   Firstname  Dateofhire   Salary      Bonus
 Bernstein  Michael    1991-05-25   98000.00    19600.00 

虽然还有3条记录应该在这里。我不明白我在代码中做错了什么,它只给我一条输出线。我检查了哪里是正确的逻辑,并且奖金的计算也是正确的。

唯一的问题是它只显示一行输出,无论我使用奖励= 20还是奖金= 5

任何建议。

1 个答案:

答案 0 :(得分:1)

问题是你的返回值是标量而不是向量。

如果要返回表格和标量值。

CREATE PROCEDURE proc_name 
                 @param int out
AS
BEGIN
    SET @param = value

    SELECT ... FROM [Table] WHERE Condition
END
GO