--年级与学校
alter table 年级 add constraint FK_年级_学校 foreign key(xxdm) references 学校(xxdm)--班级与年级
alter table 班级 add constraint FK_班级_年级 foreign key(njbj) references 学校(njbj)--班级与学校
alter table 班级 add constraint FK_班级_学校 foreign key(xxdm) references 学校(xxdm)
alter table 年级 add constraint FK_年级_学校 foreign key(xxdm) references 学校(xxdm)--班级与年级
alter table 班级 add constraint FK_班级_年级 foreign key(njbj) references 学校(njbj)--班级与学校
alter table 班级 add constraint FK_班级_学校 foreign key(xxdm) references 学校(xxdm)
解决方案 »
- 关于函数的一个问题。
- SQL 递归(语句分析)
- 触发器 表A中ID(主键) 表B中ID(外键) 当表A 改变时表B中ID也改变
- 关于SQL中传参数的问题
- The operation could not be performed because the OLE DB provider 'MSDAORA' was unable to begin a distributed transaction.
- 把excel转到sql出现null?
- 为什么我的服务无法启动
- 请问怎样用SQL语句修改表的关键字?
- 企业管理器可以连接上,用查询分析器连不上,我该怎么办??
- lujun快来帮帮我,
- 求一句SQL语句!
- 如何把access和文本文件中的数据倒入sql server 2000
alter table 年级 add constraint FK_年级_学校 foreign key(xxdm) references 学校(xxdm)
go--班级与年级(因为年级的bjbh不是主键/唯一键,所以得用触发器)
create trigger tr_constraing on 年级
for update,delete
as
if exists(select 1 from 班级 a,deleted b where a.njbh=b.njbh)
rollback tran
go--班级与学校
alter table 班级 add constraint FK_班级_学校 foreign key(xxdm) references 学校(xxdm)
--班级与年级(因为年级的bjbh不是主键/唯一键,所以得用触发器)
create trigger tr_constraing on 年级
for update,delete
as
if exists(select 1 from 班级 a,deleted b where a.xxdm=b.xxdm and a.njbh=b.njbh )
rollback tran
go
对了,我如何返回信息给用户-报错信息?
for update,delete
as
if exists(select 1 from 班级 a,deleted b where a.xxdm=b.xxdm and a.njbh=b.njbh )
begin
raiserror('要删除的年级已经有班级引用,不能删除',1,16)
rollback tran
end
go
2.还有,比如说我同时删除4条记录,但是只有两条能够删除,另外两条不能删除,
那如何知道到底是那两条不能删除?
删除操作
}
catch(SqlException exp)
{
例外处理}
现在我得删除操作在try语句块里成功退出,而没有我理解的那样进入catch操作,就没有办法进行catch处理。
create trigger tr_constraing on 年级
for update,delete
as
declare @s varchar(8000)
set @s=''
select @s=@s+','+cast(a.xxdm as varchar)+'--'+cast(a.njbh as varchar)
from 班级 a,deleted b
where a.xxdm=b.xxdm and a.njbh=b.njbh
if @@rowcount>0
begin
set @s=stuff(@s,1,1,'')
raiserror('下级年级已经被引用:
%s
不能删除',1,16,@s)
rollback tran
end
go