各位好心的DFW,帮帮我吧!这个触发器有什么问题?(10分)

  • 主题发起人 主题发起人 hurryman
  • 开始时间 开始时间
H

hurryman

Unregistered / Unconfirmed
GUEST, unregistred user!
这个触发器有什么问题?
好不容易才找到工作,放假都不能休息没办法呀,帮帮我吧
1,
假如我想在用户插入一条记录时,数据库自动将NAME改为
'职工'这两个字,我写了一个触发器,但没有效果呀
大家帮我看看,用游标也可以
2,假如我的A表里有一条M记录,当我想B表里添加一条N记录
的同时我想删除掉A表里的M着条记录,这个触发器咋写?
use worker

if exists (select name from sysobjects where name='adduser2' and type='p')
drop procedure adduser2
go

if exists (select name from sysobjects where name='del1' and type='p')
drop procedure del1
go

if exists (select * from sysobjects where id = object_id(N'[dbo].[us]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[us]
GO


CREATE TABLE [dbo].[us] (
[name] [varchar] (10) not null,
[sex] [char] (4) not null )
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

create PROCEDURE adduser2
(@name varchar(10) ,
@sex char(4),
@retmsg varchar(20) output
)
AS

if @name in (select name from us where name=@name)
begin
select retmsg='该职工以存在了'
return 0
end
begin tran
insert into us (name,sex) values (@name ,@sex)
commit tran
select @retmsg='该职工资料添加成功'
GO
select * from us
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


create trigger del1 on us
for update
as
update us set name='职工'
GO
select * from us
GO

 
1.create trigger del1 on us
after insert
as
update us set name='职工'
go
 
第二个问题呢?
 
2.
create trigger del1 on B
after insert
as
if N in (select N from inserted) -- 或者if (select count(*) from inserted where xxx=N)<>0
delete A where XXX=M
go
 
后退
顶部