现有三个表。表STEP的一个字段StepID 对应表Part的多个PARTID PART表中的每个字段 在表[2440ZYKFB]上以字段形式存在
删除Step记录我用的是一个存储过程[lue]
SQL语句大概这样
If exists(select name from sysobjects where name='drop_Step' and type='p')
drop Proc drop_Step
go
create Proc drop_Step
@StepID char(4)
as
delete from lyt_test_Problem where PartId in (select PartID from lyt_test_Part where StepId=@StepID)
delete from lyt_test_Solution where PartId in (select PartID from lyt_test_Part where StepId=@StepID)
delete from lyt_test_Part where StepId=@StepID
delete from lyt_test_step where StepId=@StepID
上面红色字体部分我删除了一个STEPID,并可能删除了多条PARTID记录。
开始的时候我在part表建的触发器[DELETE]大概是这样的
Man1983 于 papozhe.com 2007-8-13
if exists(select name from sysobjects where name='tr_del_part' and type='tr')
drop trigger tr_del_part
go
create trigger tr_del_part
on lyt_test_Part
for delete
as
declare
@PartID char(4),
@Part_Ename varchar(50),
@StrSql varchar(80),
@TableName varchar(30),
@ProductID varchar(30)
select @ProductID=ProductID,@PartID=PartID,@Part_Ename=Part_Ename from deleted
BEGIN
select @TableName=English_Name from lyt_test_product where productID=@ProductID
delete from lyt_test_problem where PartID=@PartID
delete from lyt_test_solution where PartID=@PartID
set @StrSql='ALTER TABLE ['+@TableName+'] drop column '+ @Part_Ename
print @StrSql
exec (@StrSql)
END
这样我在删除条记录的时候是我完全可以的
,包括蓝色部分的,表[2440ZYKFB]中drop对应的删掉的那个PartName
但是当stepid包含多个partid时候,删除时候
表[2440ZYKFB]只能去掉第一个对应part表的记录,关键是没循环起来,
下面是解决方案。。。
Man1983 于 papozhe.com 2007-8-13
if exists(select name from sysobjects where name='tr_del_part' and type='tr')
drop trigger tr_del_part
go
create trigger tr_del_part on lyt_test_Part
for delete
as
declare @PartID char(4),@Part_Ename varchar(50),@StrSql varchar(80),@TableName varchar(30),@ProductID varchar(30)
declare c_1 cursor for SELECT ProductID,PartID,Part_Ename FROM deleted
open c_1
fetch next from c_1 into @ProductID,@PartID,@Part_Ename
while @@fetch_status = 0
BEGIN
select @TableName=English_Name from lyt_test_product where productID=@ProductID
delete from lyt_test_problem where PartID=@PartID
delete from lyt_test_solution where PartID=@PartID
set @StrSql='ALTER TABLE ['+@TableName+'] drop column '+ @Part_Ename
print @StrSql
exec (@StrSql)
fetch next from c_1 into @ProductID,@PartID,@Part_Ename
END
--=====释放
close c_1
deallocate c_1
Man1983 于 papozhe.com 2007-8-13