SQL Server存储过程错误(员工加薪)

时间:2015-03-29 19:23:02

标签: sql-server sql-server-2008 stored-procedures sql-server-2012

我正在尝试进行存储过程,我给员工加薪,但我似乎不明白我做错了什么,它给了我以下错误

  

Msg 8115,Level 16,State 8,Procedure ClerkPayRise,Line 8
  算术溢出错误将数字转换为数据类型数字。

我的存储过程的代码是:

CREATE PROCEDURE ClerkPayRise (@clerkID      VARCHAR (50),
                               @clerkPayrate DECIMAL(3, 2))
AS
BEGIN
   UPDATE clerks
   SET clerkPayrate = clerkPayrate + @clerkPayrate
   WHERE clerkID = @clerkID

   IF @@ROWCOUNT = 1
        SELECT 'The Hourly Pay Rise for Clerk: ' + (SELECT clerkFName + ' ' + clerkSName
                                                    FROM   clerks
                                                    WHERE  clerkID = @clerkID) + 'Was Successful'
   ELSE
        SELECT ' The Increase in Hourely Pay rate Failed, No Clerk with ID ;   ' + CAST(@clerkID AS VARCHAR (50))
END

EXECUTE ClerkPayRise
  clerk01,
  0.25
GO

DROP PROCEDURE ClerkPayRise 

我使用的示例数据如下:

CREATE TABLE clerks
(
     clerkID      VARCHAR (20) NOT NULL,
     clerkFName   VARCHAR (20),
     clerkSName   VARCHAR (20),
     clerkPayrate DECIMAL(3, 2),
     PRIMARY KEY (clerkID),
);

INSERT INTO clerks
VALUES ('clerk01' ,'Alex','Richards',7.49);

INSERT INTO clerks
VALUES ('clerk02', 'Jason','Elliot',8.25);

INSERT INTO clerks
VALUES ('clerk03', 'Mark','Alleyne',8.25);

INSERT INTO clerks
VALUES ('clerk04', 'Ricky','Ponting',8.00);

INSERT INTO clerks
VALUES ('clerk05', 'Nick','Lamb',8.75);

我将非常感谢任何指导和帮助

2 个答案:

答案 0 :(得分:0)

你犯了一些错误,比如

  1. 创建TABLE职员( clerkID varchar(20)NOT NULL, clerkFName varchar(20), clerkSName varchar(20), clerkPayrate decimal(3,2), PRIMARY KEY(clerkID), );
  2. 在" PRIMARY KEY(clerkID)"之后的此查询中不应该有任何逗号","。

    1. 您要连续分配五个值,但您的表只有四列。
    2. INSERT INTO职员VALUES(' clerk01',' Mgr1',' Alex',' Richards',7.49); 这个查询是错误的。它分配了五个值而不是四个

      如果存储过程中有任何问题,请进行更正以便我可以帮助您。

答案 1 :(得分:0)

在存储过程主体和存储过程执行语句之后,您缺少GO批处理终结符。这会导致EXEC语句成为存储过程的一部分,因此存储的proc会递归调用自身,直到发生算术溢出。

CREATE PROCEDURE ClerkPayRise (@clerkID      VARCHAR (50),
                               @clerkPayrate DECIMAL(3, 2))
AS
BEGIN
   UPDATE clerks
   SET clerkPayrate = clerkPayrate + @clerkPayrate
   WHERE clerkID = @clerkID

   IF @@ROWCOUNT = 1
        SELECT 'The Hourly Pay Rise for Clerk: ' + (SELECT clerkFName + ' ' + clerkSName
                                                    FROM   clerks
                                                    WHERE  clerkID = @clerkID) + 'Was Successful'
   ELSE
        SELECT ' The Increase in Hourely Pay rate Failed, No Clerk with ID ;   ' + CAST(@clerkID AS VARCHAR (50))
END
GO --need this terminator

EXECUTE ClerkPayRise
  'clerk01', --also enclose string literals in single quotes
  0.25
GO