pyodbc - 以前的sql不是查询

时间:2013-08-19 20:26:32

标签: sql-server pyodbc

我在尝试将数据插入表格时遇到上述错误,我也尝试了SET NOCOUNT ON选项,但仍然遇到同样的问题,

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    

1 个答案:

答案 0 :(得分:0)

您需要执行第一个select [xxx] into [new_tablename] from [yyy] 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