子查询返回的值不止一个问题 100
setANSI_NULLSONsetQUOTED_IDENTIFIERONgoALTERTRIGGER[dbo].[trigger_delete_BackWords]ON...
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [dbo].[trigger_delete_BackWords]
ON [dbo].[BackWords]
INSTEAD OF DELETE
AS
/* 定义触发器使用的变量 */
DECLARE
@B_ID Int,
@fNodeCount Int
/* 把传送的需要删除的B_ID键值赋值给@B_ID变量 */
/* 开始事务 */
BEGIN TRAN delete_BackWords
Set @B_ID = (Select B_ID From deleted)
/* 保存删除前保存点,防止出错 */
Save Tran my_Save1
/* 首先判断子类表Acquirement中是否有所属内容 */
Set @fNodeCount = (Select Count(*) From Acquirement Where Acquirement.B_ID = @B_ID)
If @fNodeCount > 0
Begin
Delete From Acquirement Where B_ID = @B_ID
Delete From BackWords Where B_ID = @B_ID
End
Else
Begin
Delete From BackWords Where B_ID = @B_ID
End
If @@Error = 0
Commit Transaction
Else
Begin
Rollback Transaction my_Save1
Raiserror('删除出现错误',16,1)
End
这部分是触发器的,主要是级联删除同个B_ID所对应在Acquirement表中的外键,我懒得再写了,大家都知道现在是修改...
然后我想把同个用户(Sh_ID)所连接在BackWords表中数据删除
代码如下
delete from BackWords where B_ID in(select B_ID from BackWords where Sh_ID=10)
于是出现如下提示
消息 512,级别 16,状态 1,过程 trigger_delete_BackWords,第 15 行
子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。
各位最好快点回复啊,现在时间很赶。。。。。。
Acquirement表 A_ID A_title A_content Sh_ID B_ID
BackWords表 B_ID B_content Sh_ID 展开
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [dbo].[trigger_delete_BackWords]
ON [dbo].[BackWords]
INSTEAD OF DELETE
AS
/* 定义触发器使用的变量 */
DECLARE
@B_ID Int,
@fNodeCount Int
/* 把传送的需要删除的B_ID键值赋值给@B_ID变量 */
/* 开始事务 */
BEGIN TRAN delete_BackWords
Set @B_ID = (Select B_ID From deleted)
/* 保存删除前保存点,防止出错 */
Save Tran my_Save1
/* 首先判断子类表Acquirement中是否有所属内容 */
Set @fNodeCount = (Select Count(*) From Acquirement Where Acquirement.B_ID = @B_ID)
If @fNodeCount > 0
Begin
Delete From Acquirement Where B_ID = @B_ID
Delete From BackWords Where B_ID = @B_ID
End
Else
Begin
Delete From BackWords Where B_ID = @B_ID
End
If @@Error = 0
Commit Transaction
Else
Begin
Rollback Transaction my_Save1
Raiserror('删除出现错误',16,1)
End
这部分是触发器的,主要是级联删除同个B_ID所对应在Acquirement表中的外键,我懒得再写了,大家都知道现在是修改...
然后我想把同个用户(Sh_ID)所连接在BackWords表中数据删除
代码如下
delete from BackWords where B_ID in(select B_ID from BackWords where Sh_ID=10)
于是出现如下提示
消息 512,级别 16,状态 1,过程 trigger_delete_BackWords,第 15 行
子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。
各位最好快点回复啊,现在时间很赶。。。。。。
Acquirement表 A_ID A_title A_content Sh_ID B_ID
BackWords表 B_ID B_content Sh_ID 展开
2个回答
展开全部
这句话有问题:
Set @B_ID = (Select B_ID From deleted)
当删除的记录不止一条的时候,逻辑表deleted里面有不止一条记录。用SET @B_ID = ……就会出错。
改动最小的修改,可以按照楼上说的用循环解决,相当于Oracle里面的行级触发器。但SQL Server不支持行级触发,事实上行级触发的性能也非常差。
另一种修改,就是用IN来删除:
ALTER TRIGGER [dbo].[trigger_delete_BackWords]
ON [dbo].[BackWords]
INSTEAD OF DELETE
AS
/* 把传送的需要删除的B_ID键值赋值给@B_ID变量 */
/* 开始事务 */
BEGIN TRAN delete_BackWords
/* 保存删除前保存点,防止出错 */
Save Tran my_Save1
/* 删除子类表Acquirement中所有的所属内容 */
Begin
Delete From Acquirement Where B_ID IN (SELECT b_id FROM deleted)
/* 删除BackWords中要删除的内容 */
Delete From BackWords Where B_ID IN (SELECT b_id FROM deleted)
End
If @@Error = 0
Commit Transaction
Else
Begin
Rollback Transaction my_Save1
Raiserror('删除出现错误',16,1)
End
Set @B_ID = (Select B_ID From deleted)
当删除的记录不止一条的时候,逻辑表deleted里面有不止一条记录。用SET @B_ID = ……就会出错。
改动最小的修改,可以按照楼上说的用循环解决,相当于Oracle里面的行级触发器。但SQL Server不支持行级触发,事实上行级触发的性能也非常差。
另一种修改,就是用IN来删除:
ALTER TRIGGER [dbo].[trigger_delete_BackWords]
ON [dbo].[BackWords]
INSTEAD OF DELETE
AS
/* 把传送的需要删除的B_ID键值赋值给@B_ID变量 */
/* 开始事务 */
BEGIN TRAN delete_BackWords
/* 保存删除前保存点,防止出错 */
Save Tran my_Save1
/* 删除子类表Acquirement中所有的所属内容 */
Begin
Delete From Acquirement Where B_ID IN (SELECT b_id FROM deleted)
/* 删除BackWords中要删除的内容 */
Delete From BackWords Where B_ID IN (SELECT b_id FROM deleted)
End
If @@Error = 0
Commit Transaction
Else
Begin
Rollback Transaction my_Save1
Raiserror('删除出现错误',16,1)
End
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询