触发器的问题(100分)

  • 主题发起人 主题发起人 谭俊峰
  • 开始时间 开始时间

谭俊峰

Unregistered / Unconfirmed
GUEST, unregistred user!
我想建立一个删除触发器,当该条纪录删除时,需要删除它的从表的所有ID=所删除的纪录的ID值,但我不知道如何获得该值。
 
CREATE TRIGGER 触发器名 FOR 主库名 BEFORE DELETE POSITION 0 AS
BEGIN
DELETE FROM 从库 where 从库.id=主库.id
end
 
create triger trigername on tablename for delete as
if @@rowcount>1
begin
ROLLBACK TRAN
raiserror('you can only delete one at a time.',16,10)
end
declare @sstoredID char(10)
select @sstoredID =str_id
from deleted /*DELETED 是伪表由SQL SERVER生成来包存删除的记录值.
delete from detailtable where str_id=@sstoredid
 
CREATE TRIGGER "DN_DTrig" ON dbo.DN FOR DELETE AS
SET NOCOUNT ON
/* * CASCADE DELETES TO 'DN_detail' */
DELETE DN_detail FROM deleted, DN_detail WHERE deleted.DN_ID = DN_detail.DN_ID

 
不用这么麻烦吧?我用ACCESS的连带删除功能就可实现你的要求,只需
一句SQL就行。
 
如果是Access,就简单了,当然不用触发器(就没有触发器)
SQL SERVER下,只能写触发器
 
Oracle下可使用触发器,也可以使用级联删除.
在定义库时加上.

Field1 VARCHAR2(20) NOT NULL REFERENCES tblOther(Field1) ON DELETE CASCADE
 
总之要用到一伪表--DELETED,它是由SQL SERVER生成来包存删除的记录值的.
 
如果是InterBase可以如下:
CREATE TRIGGER <trigger_name> FOR <MainTableName>
BEFORE DELETE AS
BEGIN
DELETE FROM <SubTableName> S WHERE Old.ID=S.ID
END

 
sorry,忘了大于号问题了。:)

如果是InterBase可以如下:
CREATE TRIGGER trigger_name FOR main_table_name
BEFORE DELETE AS
BEGIN
DELETE FROM sub_table_name S WHERE Old.ID=S.ID
END
 
我在外地,未能及时回复,请多包含,
各位的热心参与,本人不胜感激.
 

Similar threads

S
回复
0
查看
730
SUNSTONE的Delphi笔记
S
S
回复
0
查看
738
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部