SHARE
TWEET

pyodbc nextset issue

a guest Oct 15th, 2010 216 Never
  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. """)
RAW Paste Data
Top