SQL Server SQLAlchemy: select ... into having no effect

jyztefdp  于 2023-03-22  发布在  其他
关注(0)|答案(1)|浏览(93)

Is select ... into not something we're able to do in SQLAlchemy?

The following is a minimal example of something I'm trying to do - and it isn't adding a table to my d/b as expected.

driver = '{ODBC Driver 17 for SQL Server}'
server = xxxxxxxxxxxxxxxxxxxxxxxxx
database = xxxxxxxxxxxxxxxxxxxxxxxxx
authentication = 'ActiveDirectoryInteractive'
username = xxxxxxxxxxxxxxxxxxxxxxxxx

conn = urllib.parse.quote_plus(
    'DRIVER=' + driver +
    ';SERVER=' + server +
    ';DATABASE=' + database +
    ';UID=' + username +
    ';AUTHENTICATION=' + authentication + ';'
)

engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(conn))

with engine.connect() as cx:
    cx.execute(text('''
        select
            name
        into testing.test
        from core.person
    '''))

It's failing silently - I'm not getting any error message. And other queries that I run are executing fine.

Are we not able to do select ... into queries using text in this way? If not, is there an alternative besides separate create table and insert into statements?

qojgxg4l

qojgxg4l1#

@GordThompson's suggestion fixed this for me, swapping engine.connect() for engine.begin() :

with engine.begin() as cx:
    cx.execute(text('''
        select
            name
        into testing.test
        from core.person
    '''))

The commit behaviour of the two functions differs, as described in more detail by Gord in this answer .

相关问题