create trigger tr_del_check on RegisterInfo for delete as declare @check int select @check=p_no from deleted if exists( MedirecorderInfo.p_no=@check) begin rollback transaction raiserror('该挂号记录有相关联的病历记录,无法删除', 16, 1) end 运行有错误 请帮我修改
时间: 2024-03-21 16:43:54 浏览: 60
sql.rar_exec select_select_sql select from exec_普通sql大全
在该触发器中,MedirecorderInfo 表的判断条件不正确,应该使用 EXISTS 子句,而不是直接使用表名和字段名。同时,在触发器中抛出错误时,需要使用 THROW 语句,而不是 RAISERROR。以下是修改后的代码:
```
CREATE TRIGGER tr_del_check ON RegisterInfo
FOR DELETE
AS
BEGIN
DECLARE @check INT
SELECT @check = p_no FROM deleted
IF EXISTS (SELECT 1 FROM MedirecorderInfo WHERE p_no = @check)
BEGIN
ROLLBACK TRANSACTION
THROW 50000, '该挂号记录有相关联的病历记录,无法删除', 1
END
END
```
请注意,触发器的实际效果需要根据具体情况进行测试和验证。
阅读全文