问下,触发器里面究竟怎么判断字段为空?

山河相制2025 2014-10-20 08:07:27
比如
select Cno from Xuanke where Sno=@Sno
我用if判断
if(select Cno from Xuanke where Sno=@Sno)
总是返回1,然后if就开始执行了
就算写成count(Cno),返回值也一直是1
究竟怎么判断,我弄了一整天,百度了半天,都找不到方法
我主要是判断该学生在选课表里是否已有选课记录,有选课记录就判断上课时间冲突,没有就end不执行任何操作
...全文
1076 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
山河相制2025 2014-10-20
  • 打赏
  • 举报
回复
引用 6 楼 alimake 的回复:



alter trigger screentime
on Xuanke
instead of  insert
  as 
  begin
  begin tran

    if exists(select * from Xuanke  as a join inserted as b on a.Sno=b.Sno  and  a.Cno is not null)
      begin
        print '改学生已有课'
        rollback tran
      end  
      else 
      begin
      insert into xuanke select * from inserted
      print'插入成功'
      commit
      end
  end
go   

insert into Xuanke values(101,01,80)
insert into Xuanke values(101,02,80)
--这2句进行分别执行。第一句没问题。但是执行第二句就会提示已经有课。会插入不成功
这里必须用BEFORE 触发器。用FOR 触发器。表示触发器会在插入之后执行。你已经把记录插入到表里面了。 这样你在用IF 判断。表里面肯定存在记录啊。所有都是返回1
额,请问这个before触发器怎么办?sql server 2008 r2 好像没有这个
山河相制2025 2014-10-20
  • 打赏
  • 举报
回复
引用 6 楼 alimake 的回复:



alter trigger screentime
on Xuanke
instead of  insert
  as 
  begin
  begin tran

    if exists(select * from Xuanke  as a join inserted as b on a.Sno=b.Sno  and  a.Cno is not null)
      begin
        print '改学生已有课'
        rollback tran
      end  
      else 
      begin
      insert into xuanke select * from inserted
      print'插入成功'
      commit
      end
  end
go   

insert into Xuanke values(101,01,80)
insert into Xuanke values(101,02,80)
--这2句进行分别执行。第一句没问题。但是执行第二句就会提示已经有课。会插入不成功
这里必须用BEFORE 触发器。用FOR 触发器。表示触发器会在插入之后执行。你已经把记录插入到表里面了。 这样你在用IF 判断。表里面肯定存在记录啊。所有都是返回1
丢人了,这个都忘了
xiaodongni 2014-10-20
  • 打赏
  • 举报
回复



alter trigger screentime
on Xuanke
instead of  insert
  as 
  begin
  begin tran

    if exists(select * from Xuanke  as a join inserted as b on a.Sno=b.Sno  and  a.Cno is not null)
      begin
        print '改学生已有课'
        rollback tran
      end  
      else 
      begin
      insert into xuanke select * from inserted
      print'插入成功'
      commit
      end
  end
go   

insert into Xuanke values(101,01,80)
insert into Xuanke values(101,02,80)
--这2句进行分别执行。第一句没问题。但是执行第二句就会提示已经有课。会插入不成功
这里必须用BEFORE 触发器。用FOR 触发器。表示触发器会在插入之后执行。你已经把记录插入到表里面了。 这样你在用IF 判断。表里面肯定存在记录啊。所有都是返回1
山河相制2025 2014-10-20
  • 打赏
  • 举报
回复
引用 4 楼 alimake 的回复:
select @Sno=Sno from inserted select @Cno1=Cno from inserted 你用这个有点问题。如果每次插入一行记录没问题,如果插入多行记录没法匹配啊。用SELECT 赋值虽然不报错。 但是只有最后一行有效。 比如表A 学号 2008 2009 2010 这样记录。一般只有最后一行对赋值有效。 换一下 if exists (select * from inserted as a join xuanke as b on a.sno=b.sno and b.cno is not null ) (这个表示就有课了,当然这个只能判断有学生有课了。不能说明所有学生友课。。需要在判断上课时间是否冲突)
关于之前的方法我在触发器外试了一下,可以判断非空执行IF里面的语句,空则不执行。 但是在触发器里面就不行,请问下这有什么原因吗?还是我软件出问题了?
xiaodongni 2014-10-20
  • 打赏
  • 举报
回复
select @Sno=Sno from inserted select @Cno1=Cno from inserted 你用这个有点问题。如果每次插入一行记录没问题,如果插入多行记录没法匹配啊。用SELECT 赋值虽然不报错。 但是只有最后一行有效。 比如表A 学号 2008 2009 2010 这样记录。一般只有最后一行对赋值有效。 换一下 if exists (select * from inserted as a join xuanke as b on a.sno=b.sno and b.cno is not null ) (这个表示就有课了,当然这个只能判断有学生有课了。不能说明所有学生友课。。需要在判断上课时间是否冲突)
山河相制2025 2014-10-20
  • 打赏
  • 举报
回复
引用 2 楼 alimake 的回复:
一般来说。你用if exists(select 2 from Xuanke where Sno=@Sno)(一般用一个常数 表示是否存在记录。不用写列。可以提高下效率。) 就可以判断。 但是这个要看你XUANKE 这个表的结构。如果学生08没有选课。在XUANKE 表里面是不存在SNO=08的记录。还是有记录。 但是CNO 是空值。我上面的代码是实现第一种情况。一般选课表也都是这么做的。 如果是第二种情况。加个条件。 if exists(select 2 from Xuanke where Sno=@Sno and cno is not null)
你好,我用了第二种方法,但是结果还是一样,我把代码和记录贴上来 create table Xuanke ( Sno int , Cno int, Score decimal, primary key (Sno,Cno), foreign key(Sno) references Students(Sno), foreign key(Cno) references Course(Cno) ) go create trigger screentime on Xuanke for insert as begin declare @Sno int declare @Cno1 int declare @Cno2 int declare @num int select @Sno=Sno from inserted select @Cno1=Cno from inserted print 1 if exists(select * from Xuanke where Sno=@Sno and Cno is not null) begin print 2 rollback tran end end go 这是执行的结果 1 2 消息 3609,级别 16,状态 1,第 1 行 事务在触发器中结束。批处理已中止。
xiaodongni 2014-10-20
  • 打赏
  • 举报
回复
一般来说。你用if exists(select 2 from Xuanke where Sno=@Sno)(一般用一个常数 表示是否存在记录。不用写列。可以提高下效率。) 就可以判断。 但是这个要看你XUANKE 这个表的结构。如果学生08没有选课。在XUANKE 表里面是不存在SNO=08的记录。还是有记录。 但是CNO 是空值。我上面的代码是实现第一种情况。一般选课表也都是这么做的。 如果是第二种情况。加个条件。 if exists(select 2 from Xuanke where Sno=@Sno and cno is not null)
还在加载中灬 2014-10-20
  • 打赏
  • 举报
回复
if EXISTS(select Cno from Xuanke where Sno=@Sno) --有 ELSE --没有

22,299

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧