code_junkie

How do I output progress messages from a SELECT statement?

Nov 11th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. DECLARE @total_rows int
  2.  
  3. SELECT @total_rows = count(*)
  4. FROM Source_Table
  5.  
  6. WHILE @total_rows > (SELECT count(*) FROM Target_Table)
  7. BEGIN
  8. SET rowcount 1000
  9.  
  10. print 'inserting 1000 rows'
  11.  
  12. INSERT Target_Table
  13. SELECT *
  14. FROM Source_Table s
  15. WHERE NOT EXISTS( SELECT 1
  16. FROM Target_Table t
  17. WHERE t.id = s.id )
  18. END
  19.  
  20. set rowcount 0
  21. print 'done'
  22.  
  23. DECLARE @min_id int,
  24. @max_id int,
  25. @start_id int,
  26. @end_id int
  27.  
  28. SELECT @min_id = min(id) ,
  29. @max_id = max(id)
  30. FROM Source_Table
  31.  
  32. SELECT @start_id = @min_id ,
  33. @end_id = @min_id + 1000
  34.  
  35. WHILE @end_id <= @max_id
  36. BEGIN
  37.  
  38. print 'inserting id range: ' + convert(varchar,@start_id) + ' to ' + convert(varchar,@end_id)
  39.  
  40. INSERT Target_Table
  41. SELECT *
  42. FROM Source_Table s
  43. WHERE id BETWEEN @start_id AND @end_id
  44.  
  45. SELECT @start_id = @end_id + 1,
  46. @end_id = @end_id + 1000
  47. END
  48.  
  49. set rowcount 0
  50. print 'done'
Add Comment
Please, Sign In to add comment