存储过程或触发器以强制执行输入数据

时间:2013-05-25 05:32:18

标签: sql-server-2008 sql-server-2008-r2 sql-server-2012

我的教授要求我使用存储过程或触发器来确保每次插入新员工时,员工至少分配了一台计算机,而不是空。 http://i1294.photobucket.com/albums/b618/uRsh3RRaYm0nD/Capturescreenie_zps67ab757a.jpg 表格布局为:

电脑桌

SerialNumber_PK,
Make,
Model

Computer_Employee表

SerialNumber_PK_FK,
EmployeeNumber_PK_FK,
DateAssigned

员工表

EmployeeNumber_PK,
FirstN,
LastN,
Department_FK,
Phone,
Email,

我决定使用存储过程,但是我被卡住了,我不知道这是否正确。我只有这个,任何帮助将不胜感激。谢谢。

CREATE PROCEDURE sp_AddEmployee
(   @EmployeeNumber Int,
    @FirstName  Char(25),
    @LastName   Char(25),
    @Department Char(35),
    @Phone  Char(12),
    @Email  VarChar(100))
AS
DECLARE @rowcount AS Int

SELECT @rowcount = COUNT(*)
FROM EMPLOYEE AS E
WHERE E.EmployeeNumber = @EmployeeNumber
IF @rowcount > 0
    Begin
        PRINT ' '
        PRINT 'The employee with employee number: '
        PRINT ' '
        PRINT Str(@EmployeeNumber)
        PRINT ' '
        PRINT 'already exists in the EMPLOYEE table.'
        RETURN;
    END;

DECLARE @pc_rowcount AS Int
SELECT @pc_rowcount = COUNT(*)
FROM COMPUTER_ASSIGNMENT AS C
WHERE C.EmployeeNumber = @EmployeeNumber
IF @pc_rowcount = 0
    BEGIN
        PRINT ' '
        PRINT ' '
        PRINT 'A computer serial number must be assigned to the added employee'
        PRINT 'number in the computer assignment table'
        PRINT ' '
        PRINT ' '
        RETURN;
    END;
IF @pc_rowcount > 0
    BEGIN 
        INSERT INTO EMPLOYEE
                (EmployeeNumber, FirstName, LastName, Department, Phone,
                 Email)
        VALUES(@EmployeeNumber, @FirstName, @LastName, @Department, @Phone,
                @Email);
        PRINT '*******************************************************'
        PRINT ' '
        PRINT 'The employee with employee number: '
        PRINT ' '
        PRINT Str(@EmployeeNumber)
        PRINT ' '
        PRINT 'has been added to the EMPLOYEE table.'
        PRINT ' '
        PRINT '*******************************************************'
    END;

2 个答案:

答案 0 :(得分:0)

给出的答案并不多,但使用交易存储过程可能会为您节省大量工作

刚开始交易 - >插入员工 - >将最后一个插入ID存储在某个变量中 - >在计算机表serialNumber和Computer_Employee序列号之间做一个区别,并取上它的top()并将其存储在变量中 - >使用当前日期函数

将这些变量插入到computer_Employee表中

你可以用触发器做同样的事情,但我个人在获取插入ID时从未信任过使用触发器,尽管它总是起作用,但

这也假设您的employeeID是auto_Inc

编辑:猜测它不是你在你的参数列表中包含它,所以只需使用

答案 1 :(得分:0)

一般规则:当您要检查是否存在数据时,请使用EXISTS,但不需要明确的COUNT。不要命名存储过程sp_MyName

编辑:添加了额外信用额度的交易。

create procedure AddEmployee(
  @EmployeeNumber Int,
  @FirstName VarChar(25),
  @LastName VarChar(25),
  @Department VarChar(35),
  @Phone VarChar(12),
  @Email VarChar(100) )
as

  set nocount on

  begin transaction

  if exists ( select 42 from Employee where EmployeeNumber = @EmployeeNumber )
    begin
    rollback transaction
    RaIsError( 'Employee #' + Cast( @EmployeeNumber as VarChar(10) ) + ' already exists.', 13, 0 )
    end

  if not exists ( select 42 from Computer_Assignment where EmployeeNumber = @EmployeeNumber )
    begin
    rollback transaction
    RaIsError( 'Employee #' + Cast( @EmployeeNumber as VarChar(10) ) + ' has no computer.', 13, 1 )
    end

  insert into Employee ( EmployeeNumber, FirstName, LastName, Department, Phone, Email )
    values ( @EmployeeNumber, @FirstName, @LastName, @Department, @Phone,  @Email );

  commit transaction

对不起,您发现StackOverflow无用。我想最初的17分钟等待某人询问细节是非常难以忍受的。然后,在你再次看到你的教授只是打破骆驼背部的稻草之前,没有人为你做任务。在出门的时候随意撤回你的问题。

并且不要相信你所教的关于级联删除的一切都是个好主意。