import pyodbc
# 连接到 SQL Server 数据库
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=your_server;DATABASE=your_database;UID=your_username;PWD=your_password')
cursor = conn.cursor()
# 创建一个临时表
cursor.execute("SELECT * INTO temp_table FROM first_table")
conn.commit()
# 查找要合并的表名
table_names = ['second_table', 'third_table', 'fourth_table']
# 合并其他表到临时表
for table_name in table_names:
cursor.execute(f"INSERT INTO temp_table SELECT * FROM {table_name}")
conn.commit()
# 关闭连接
cursor.close()
conn.close()
mysql合并多个表
最新推荐文章于 2024-08-20 06:52:22 发布
本文介绍了如何通过Python的pyodbc模块连接到SQLServer数据库,创建临时表,从多个表中获取数据并将其合并到临时表中,最后确保了连接的关闭。
8788

被折叠的 条评论
为什么被折叠?



