关于SQL触发器的问题 ?同时删除多行,这些行插入到另一张表中?
表一classlist,表二delclasslist,删除表一并把删除的所有行插入到表二中,表一classlist有个触发器classlist_delete表二delcl...
表一classlist,表二delclasslist,删除表一并把删除的所有行插入到表二中,
表一classlist有个触发器classlist_delete
表二delclasslist与表一字段一样,且多了一个cid(int型,表二的cid等于表一的id,表二的id为自增型)
删除表一时,会有fid字段相同的多行,如果是一行好用,多行就在插入到表二中会报错如下:
“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”
触发器部分内容如下:
declare
@ID int,
@StuID varchar(20),
@Student varchar(50),
@fid int,
@StuClass int,
@Teacher int,
set @ID=(select id from deleted)
set @StuID=(select stuid from deleted)
set @Student=(select student from deleted)
set @fid=(select fid from deleted)
set @StuClass=(select stuclass from deleted)
set @Teacher=(select teacher from deleted)
insert into delclasslist(StuID,Student,fid,StuClass,Teacher,cid) values(@StuID,@Student,@fid,@StuClass,@Teacher,@id)
END 展开
表一classlist有个触发器classlist_delete
表二delclasslist与表一字段一样,且多了一个cid(int型,表二的cid等于表一的id,表二的id为自增型)
删除表一时,会有fid字段相同的多行,如果是一行好用,多行就在插入到表二中会报错如下:
“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”
触发器部分内容如下:
declare
@ID int,
@StuID varchar(20),
@Student varchar(50),
@fid int,
@StuClass int,
@Teacher int,
set @ID=(select id from deleted)
set @StuID=(select stuid from deleted)
set @Student=(select student from deleted)
set @fid=(select fid from deleted)
set @StuClass=(select stuclass from deleted)
set @Teacher=(select teacher from deleted)
insert into delclasslist(StuID,Student,fid,StuClass,Teacher,cid) values(@StuID,@Student,@fid,@StuClass,@Teacher,@id)
END 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询