【问题标题】:create SQL table from existing table using pyodbc/turbodc in python在 python 中使用 pyodbc/turbodc 从现有表创建 SQL 表
【发布时间】:2020-03-04 07:08:05
【问题描述】:

我想从现有表创建一个 SQL 表。我正在使用 turbodbc 模块(与 pyodbc 非常相似)。

# connect to database
conn = turbodbc.connect(connection_string="my_connection_string")
cursor = conn.cursor()
# execute SQL code
cursor.execute((" create table Test_Puts as"
                " select * from OptionValue"
                " where call_put = 'P'"))

但是,我收到错误消息:

ODBC error
state: 42000
native error code: 156
message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'select'.

【问题讨论】:

  • 尽量把你的选择代码放在括号()中,在选择之前开始,在'P'之后结束
  • @VBoka 我得到“'('”附近的语法不正确,本机错误代码 102

标签: python sql pyodbc


【解决方案1】:

尝试使用以下语法:

select * into Test_Puts from  OptionValue where call_put = 'P'

所以,不要这样:

" create table Test_Puts as"
" select * from OptionValue"
" where call_put = 'P'"

使用这个:

" select * into Test_Puts"
" from  OptionValue"
" where call_put = 'P'"

【讨论】:

  • 嗨@thomas.mac,那么,这有帮助吗?感谢您的反馈。
  • 感谢您的回答,好处是它不会出错,但坏处是运行时间很长,所以不确定最终结果如何。有没有更有效的方式来运行代码?
  • 嗨@thomas.mac 您选择的行数是多少?
  • 6.7亿行!
猜你喜欢
  • 2022-06-10
  • 1970-01-01
  • 2019-09-02
  • 2016-01-18
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多