2013-08-19 73 views
0

我得到上述錯誤,而試圖將數據插入到表中,我嘗試了SET NOCOUNT ON選項,以及和仍然有同樣的問題,pyodbc - 以前的SQL不是查詢

import pyodbc as p 

connstr= 'DRIVER={SQL Server};SERVER=SQLEXPRESS;DATABASE=TEST;Trusted_Connection=yes;unicode_results=False' 
conn = p.connect(connstr,charset='UTF-16') 
print conn 
cursor = conn.cursor() 

try: 
    result = cursor.execute("""select col1 into tb2 from (select 1 as col1) tb1""") 
except Exception as error: 
    print error 

for each in result.fetchall(): 
    print each  

回答

0

您需要首先執行那條TSQL語句,然後從新創建的tb2表中讀取。

import pyodbc as p 

connstr= 'DRIVER={SQL Server};SERVER=.\SQLEXPRESS;DATABASE=TEST;Trusted_Connection=yes;unicode_results=False' 
conn = p.connect(connstr,charset='UTF-16') 
print conn 
cursor = conn.cursor() 

try: 
    result = cursor.execute("""select col1 into tb2 from (select 1 as col1) tb1""") 
except Exception as error: 
    print error 

try: 
    result = cursor.execute("""select col1 from tb2""") 
except Exception as error: 
    print error 

for each in result.fetchall(): 
    print each 

此外,您可能想將實例名稱更改爲.\SQLEXPRESS,而不是SQLEXPRESS