查詢表中已刪除id的方法

方法一: 創建一個滿足需求的很大的View

create   view   v_number         --創建一個view,裏面存放1到9999的數據,如果需要更大可以根據自己的情況修改  
  as  
  select   number   from    
  (    
  select   t1.b+t2.b*10+t3.b*100+t4.b*1000   number    
  from    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t1,    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t2,    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t3,    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t4    
  )   t5    
  where   number<>0  
   
   
  --用左外連接找到clothid中的cid不在v_number中的數  
  select   number    
  from   v_number   left   outer   join   YWXX on   number   =   YWID   
  where   YWID   is   null   and   number   <(select   max(YWID   )   from   YWXX )  
  order   by   number 

 

方法二:

創建一個臨時表
  declare   @temptable   table(a   int)  
       
  declare   @char     varchar(1000),@i   int,@num   int  
  set   @num=(select   max(YWID)   from   YWXX)  
  set   @i=1  
  while   @i<=@num  
  begin  
      insert   @temptable   values   (@i)  
      set   @i=@i+1  
  end  
  select   a   from   @temptable   where   a   not   in   (select  YWID  from  YWXX)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章