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