sql server 2005触发器的问题

sungj09173 2009-12-28 10:24:34

我一下代码有什么问题呢?
当我向员工表中添加数据时,不能向部门表自动添加啊



create trigger pt2
on people for insert
as
declare @d char(10)
declare @e char(10)
select @d= id from people
select @e=position from people
if @e='经理'
begin
insert into department(department_id,department_name, id) values('a','经理室',@d)
end
if @e='技术人员'
begin
insert into department(department_id,department_name ,id) values('b','技术部门',@d)
end
if @e='销售人员'
begin
insert into department(department_id,department_name ,id) values('d','销售部门',@d)
end
if @e='财务人员'
begin
insert into department(department_id,department_name ,id) values('c','财务部门',@d)
end
...全文
71 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yang_ 2009-12-28
  • 打赏
  • 举报
回复
别没测试就说不对,虽然我没测试
Yang_ 2009-12-28
  • 打赏
  • 举报
回复
触发器不是这么用的,财你的需求给你个,看看是不是你要的

create trigger pt2 
on people for insert
as

insert into department(department_id,department_name, id)
select 'a','经理室',min(id)
from inserted i
where i.position = '经理'
and not exists ( select 1 from department where department_id ='a')

insert into department(department_id,department_name, id)
select 'b','技术部门',min(id)
from inserted i
where i.position = '技术人员'
and not exists ( select 1 from department where department_id ='b')

insert into department(department_id,department_name, id)
select 'd','销售部门',min(id)
from inserted i
where i.position = '销售人员'
and not exists ( select 1 from department where department_id ='d')

insert into department(department_id,department_name, id)
select 'c','财务部门',min(id)
from inserted i
where i.position = '财务人员'
and not exists ( select 1 from department where department_id ='c')

go
sungj09173 2009-12-28
  • 打赏
  • 举报
回复
快点来帮帮我吧
我觉得可能是if语句格式有问题
sungj09173 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 acmain_chm 的回复:]
你的create table 语句是什么?是不是有外键的影响?
[/Quote]
你说的外键影响什么意思
表的创建
create table department(id char(10) primary key, department_id char(10), department_name char(20), foreign key (id) references people(id))

create table people(id char(10) primary key,name char(10),age char(4),workePlace char(20),position char(10))

sungj09173 2009-12-28
  • 打赏
  • 举报
回复
格式没问题
能运行通过
bobui 2009-12-28
  • 打赏
  • 举报
回复
这个触发器写得有格式错误吧,你都没了解什么是触发器就在用触发器了,建议先看看触发器方面的资料
sungj09173 2009-12-28
  • 打赏
  • 举报
回复
create table department(id char(10) primary key, department_id char(10), department_name char(20), foreign key (id) references people(id))

create table people(id char(10) primary key,name char(10),age char(4),workePlace char(20),position char(10))
ACMAIN_CHM 2009-12-28
  • 打赏
  • 举报
回复
你的create table 语句是什么?是不是有外键的影响?
sungj09173 2009-12-28
  • 打赏
  • 举报
回复
那应该怎么做
SQL77 2009-12-28
  • 打赏
  • 举报
回复
好像米问题呀,CHAR(10)有没有关系
--小F-- 2009-12-28
  • 打赏
  • 举报
回复
一个关联都没有??怎么触发?

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧