简体   繁体   中英

create SQL table from existing table using pyodbc/turbodc in python

I would like to create a SQL table from an existing table. I'm using the turbodbc module (which is very similar to 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'"))

However, I get the error message:

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

Try to use this syntax:

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

So, instead of this:

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

use this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM