日期:2014-05-18  浏览次数:21070 次

为表创建触发器,为什么提示对象不存在?
建表语句

SQL code
use WSCReview
create table RiverBasinRecord
(
RBR_ID int identity not null primary key,
RiverBasinType char(2) not null check(RiverBasinType like '%[0-3][1-2%'),
FatherRiverBasin int not null,
RiverBasinName nvarchar(20) not null,
unique(FatherRiverBasin,RiverBasinName)
)


触发器,目的是insert时候判断FatherRiverBasin的值是不是在RBR_ID中已有相同的,第一次用触发器不知道是不是这样写
SQL code
create trigger tri_insert_RBR
on RiverBasinRecord
for insert
as
if(select RBR_ID from RiverBasinRecord,inserted
where RiverBasinRecord.RBR_ID=inserted.FatherRiverBasin)=null
begin
raiserror('没有找到所属流域',16,8)
rollback transaction
end
go


消息 8197,级别 16,状态 4,过程 tri_insert_RBR,第 1 行
对象 'RiverBasinRecord' 不存在,或对此操作无效。

SQL Server新手请教大家

------解决方案--------------------
创建触发器的时候也加上use WSCReview,我怀疑你用的不是同一个数据库
------解决方案--------------------
SQL code

use WSCReview

create trigger tri_insert_RBR
on RiverBasinRecord
for insert
as
if(select RBR_ID from RiverBasinRecord,inserted
where RiverBasinRecord.RBR_ID=inserted.FatherRiverBasin)=null
begin
raiserror('没有找到所属流域',16,8)
rollback transaction
end
go

试试

------解决方案--------------------
探讨

确定表建好了??

------解决方案--------------------
那你 select * from RiverBasinRecord 

试试
------解决方案--------------------
搞到别的数据库去了吧
------解决方案--------------------
先说个题外话。你的if 内 肯定 走不到啊。
没有任何东西 = null。
应该判断 is null或别的