SQL触发器实现级联删除问题
有三个表Users(U_id,.....)、News(N_id,N_text,U_id)、Comments(C_id,C_text,N_id,U_id),现在要删除Use...
有三个表Users(U_id,.....)、News(N_id,N_text,U_id)、Comments(C_id,C_text,N_id,U_id),现在要删除Users中的一个用户,同时要删除用户所发表的评论(存在Comments中)、用户所发表的新闻(存在在News中)以及对其所发新闻的所有评论(存在Comments中)。我现写的触发器代码如下: CREATE TRIGGER [dbo].[TrigUsersDelete] ON [dbo].[Users] instead of DELETE AS BEGIN declare @U_id int select @U_id=U_id from deleted --删除评论 delete from Comments where U_id=@U_id delete from Comments where N_id in (select N_id from News where U_id=@U_id) --删除新闻 delete from News where U_id=@U_id --删除用户 delete from Users where U_id=@U_id END 执行delete from Users where U_id=1时出现以下错误: 子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。 我在查询分析器中执行select * from Comments where N_id in (select N_id from News where U_id=1)时能查到结果,但执行delete from Comments where N_id in (select N_id from News where U_id=1)时又会出现上述错误,这是为什么呢?
展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询