使用Snowflake Python连接器创建Pandas临时表后查询报错
Pandas写入Snowflake临时表后查询不存在的问题解决
问题场景
用Pandas的write_pandas工具向Snowflake写入临时表,之后执行查询时提示表不存在:
ProgrammingError: 002003 (42S02): SQL compilation error:
Object 'TEMP_TABLE' does not exist or not authorized.
使用的代码如下:
import snowflake.connector from snowflake.connector.pandas_tools import write_pandas with snowflake.connector.connect( account='snoflakewebsite', user='username', authenticator='externalbrowser', database='db', schema='schema' ) as con: success, nchunks, nrows, _ = write_pandas( conn=con, df=df, table_name='temp_table', auto_create_table = True, table_type='temporary', overwrite = True, database='db', schema='schema' ) cur = con.cursor() cur.execute('select * from temp_table')
解决方法
1. 使用完全限定表名查询
临时表属于当前会话,但查询时如果不指定库和schema,可能会因为上下文切换找不到表,直接用完整路径查询:
cur.execute('select * from db.schema.temp_table')
2. 确认临时表创建成功
先检查write_pandas返回的success状态,确保表确实被创建:
success, nchunks, nrows, _ = write_pandas(...) if success: cur = con.cursor() cur.execute('select * from temp_table') else: print("临时表创建失败,请检查连接或DataFrame格式")
3. 重置会话上下文
write_pandas内部可能会改变会话的库/schema上下文,创建表后重新指定当前库和schema:
success, nchunks, nrows, _ = write_pandas(...) # 重置上下文 con.cursor().execute("USE DATABASE db; USE SCHEMA schema;") cur.execute('select * from temp_table')
4. 修正table_type参数大小写
Snowflake的SQL关键字通常是大写,尝试将参数改为大写:
write_pandas( ..., table_type='TEMPORARY', ... )
内容的提问来源于stack exchange,提问作者trey hannam
相关产品推荐
相关产品推荐

