Want more features on Pastebin? Sign Up, it's FREE!
Guest

pyodbc nextset issue

By: a guest on Oct 15th, 2010  |  syntax: Python  |  size: 0.92 KB  |  views: 213  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import traceback
  2. import pyodbc
  3.  
  4. db = pyodbc.connect (r"Driver={SQL Server};Server=SVR17;Database=TDI;TrustedConnection=Yes")
  5.  
  6. def test_sql (sql):
  7.   q = db.cursor ()
  8.   try:
  9.     q.execute (sql)
  10.     print q.fetchall ()
  11.     print q.nextset ()
  12.     print q.fetchall ()
  13.   except pyodbc.Error:
  14.     print "ERROR:", traceback.format_exc ()
  15.   finally:
  16.     q.close ()
  17.  
  18. print
  19. print "SELECT-SELECT"
  20. test_sql ("""
  21. SELECT 1
  22. SELECT 2
  23. """)
  24.  
  25. print
  26. print "SELECT-INSERT-SELECT"
  27. test_sql ("""
  28. DECLARE @t TABLE (i INT)
  29. SELECT 1
  30. INSERT INTO @t VALUES (1)
  31. SELECT 2
  32. """)
  33.  
  34. print
  35. print "SELECT-INSERT-SELECT NOCOUNT"
  36. test_sql ("""
  37. SET NOCOUNT ON
  38. DECLARE @t TABLE (i INT)
  39. SELECT 1
  40. INSERT INTO @t VALUES (1)
  41. SELECT 2
  42. """)
  43.  
  44. print
  45. print "SELECT-PRINT-SELECT"
  46. test_sql ("""
  47. SELECT 1
  48. PRINT 'Hello'
  49. SELECT 2
  50. """)
  51.  
  52. print
  53. print "SELECT-RAISERROR-SELECT"
  54. test_sql ("""
  55. SELECT 1
  56. RAISERROR ('ERROR', 16, 1)
  57. SELECT 2
  58. """)
clone this paste RAW Paste Data