判斷一個區間的數據是否都存在於表中

testTable :

id_start     id_end
---------    ----------
1        10
11       14
15       30
38       45
50       60

 

 

--    在頁面上輸入一個區間的值, 如  開始:30   結束:50 
--    要判斷 30~50 這個區間內的所有數字(30,31,32,33,...,50)是否都存在testTable 表內
--    輸出不存在的數據
-- =============================================


BEGIN

 declare @start int
 declare @end int

 set @start=30
 set @end=50

 while(@start <= @end)
 begin
  if exists(select * from testTable where @start between startnum and endnum)
  begin
   print convert(varchar(20),@start)+':exists';
  end
  else
  begin
   print convert(varchar(20),@start)+':not exists';
  end
  set @start = @start + 1 
 end

END

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