如何在bcpandas的to_sql中指定SQL Server表的架构名称?
解决bcpandas指定SQL Server架构名的问题
你不需要把架构名和表名拼接在一起传入,bcpandas的to_sql函数提供了专门的schema参数来指定表所属的架构,修改代码即可解决问题:
import pandas as pd import numpy as np from bcpandas import SqlCreds, to_sql creds = SqlCreds( 'DEV_SERVER', 'DEV_DB', 'SQLServer_UserName', 'SQLServer_Password' ) df = pd.DataFrame( data=np.ndarray(shape=(10, 6), dtype=int), columns=[f"col_{x}" for x in range(6)] ) try: # 添加schema参数,替换为你实际需要的架构名称 to_sql(df, 'BCPTest', creds, index=False, if_exists='replace', schema='your_target_schema') except Exception as E: print(E)
关键说明
- 将
your_target_schema替换为你实际要使用的架构名(比如staging、sales等业务架构) - 确保你的SQL Server账号拥有该架构下的表创建、数据写入权限,否则可能触发权限类报错
内容的提问来源于stack exchange,提问作者Aritra Sarkar
相关产品推荐
相关产品推荐

