--数据结构   
  表名tb,如果修改表名,则相应修改所有数据处理中涉及到的表名tb  
  id为编号(标识字段+主键)  
  pid为上级编号  
  name为名称,后面可以自行增加其他字段.

 

/*--数据完整性检查--*/  
  --自定义函数--检测某个编码出发,是否被循环引用  
  create   function   f_chkid(@id   int)  
  returns   bit --循环,返回1,否则返回0  
  as  
  begin  
  declare   @re   bit,@pid   int  
   
  set   @re=0  
   
  --检测  
  select   @pid=pid   from   tb   where   id=@id  
  while   @@rowcount>0  
  begin  
  if   @pid=@id  
  begin  
  set   @re=1  
  goto   lbErr  
  end  
  select   @pid=pid   from   tb   where   id=@pid  
  end  
   
  lbErr:  
  return(@re)  
  end  
  go  
   
  --显示表中的那些数据不符合规范  
  select   *   from   tb   a    
  where   not   exists(select   1   from   tb   where   id=a.pid)  
  or   dbo.f_chkid(id)=1  
  go  

相关文章:

  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
  • 2021-05-02
  • 2021-11-11
  • 2021-06-09
猜你喜欢
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
相关资源
相似解决方案