建立唯一索引吧 create unique index index_name on Employees(DepartmentID)
insert into Employees(DepartmentID,col2) select 100,'c' where not exists(select * from Employees where DepartmentID=100)
若[不]存在,则将该记录插入Employees表 加个不吧?
CREATE TRIGGER TRI_IN ON Employees INSTEAD OF INSERT AS BEGIN INSERT Employees SELECT * FROM INSERTED I,Departments D WHERE I.DepartmentID =D.DepartmentID END
建索引? 应该是价格外键约束吧 在 Employees DepartmentID字段上
你应该设置Employees表中DepartmentID字段为Departments表的外键。
alter proc depart @DepartmentID int as begin if exists(select * from departments where DepartmentID=@DepartmentID) insert into Employees values(012354,'卡龙帕',1965-01-12,1,'十分大路-1-22','210005','12345678',null,@departmentID) --print'插入成功' else print '没得这种部门号' end exec depart '6'
create unique index index_name on Employees(DepartmentID)
where not exists(select * from Employees where DepartmentID=100)
加个不吧?
AS
BEGIN
INSERT Employees
SELECT * FROM
INSERTED I,Departments D
WHERE I.DepartmentID =D.DepartmentID
END
建索引?
应该是价格外键约束吧 在 Employees DepartmentID字段上
@DepartmentID int
as
begin
if exists(select * from departments
where DepartmentID=@DepartmentID)
insert into Employees values(012354,'卡龙帕',1965-01-12,1,'十分大路-1-22','210005','12345678',null,@departmentID)
--print'插入成功'
else
print '没得这种部门号'
end
exec depart '6'