0

尝试将数据插入表时出现上述错误,我也尝试了 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    
4

1 回答 1

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.

于 2013-08-19T21:36:43.427 回答