-1

我的教授要求我使用存储过程或触发器来确保每次插入新员工时,该员工至少分配了一台计算机,而不是空的。 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;
4

2 回答 2

0

使用事务的存储过程可以为您节省大量工作。

只需启动事务->插入员工->将最后一个插入ID存储在某个变量中->在计算机表序列号和Computer_Employee序列号之间做一个区别,然后将其top()存储在一个变量中->插入这两个变量使用当前日期函数进入 computer_Employee 表

你可以用触发器做同样的事情,但我个人从不相信在获取插入 ID 时使用触发器,尽管它一直有效。

这也假设您的员工 ID 是 auto_Inc

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

于 2013-05-25T05:39:48.510 回答
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

也不要相信你所学到的关于级联删除的所有内容都是一个好主意。

于 2013-06-05T01:16:57.107 回答