简体   繁体   English

使用 Python 在 SQL Server 中将表从一个数据库复制到另一个数据库

[英]Copy tables from one database to another in SQL Server, using Python

Does anybody know of a good Python code that can copy large number of tables (around 100 tables) from one database to another in SQL Server?有谁知道一个好的 Python 代码可以在 SQL Server 中将大量表(大约 100 个表)从一个数据库复制到另一个数据库?

I ask if there is a way to do it in Python, because due to restrictions at my place of employment, I cannot copy tables across databases inside SQL Server alone.我问是否有办法在 Python 中做到这一点,因为由于我工作地点的限制,我不能单独在 SQL Server 内的数据库之间复制表。

Here is a simple Python code that copies one table from one database to another.这是一个简单的 Python 代码,它将一个表从一个数据库复制到另一个数据库。 I am wondering if there is a better way to write it if I want to copy 100 tables.如果我想复制 100 个表,我想知道是否有更好的方法来编写它。

print('Initializing...')

import pandas as pd
import sqlalchemy
import pyodbc

db1 = sqlalchemy.create_engine("mssql+pyodbc://user:password@db_one")
db2 = sqlalchemy.create_engine("mssql+pyodbc://user:password@db_two")

print('Writing...')

query = '''SELECT * FROM [dbo].[test_table]'''
df = pd.read_sql(query, db1)
df.to_sql('test_table', db2, schema='dbo', index=False, if_exists='replace')

print('(1) [test_table] copied.')

SQLAlchemy is actually a good tool to use to create identical tables in the second db: 实际上,SQLAlchemy是一个很好的工具,可用于在第二个数据库中创建相同的表:

table = Table('test_table', metadata, autoload=True, autoload_with=db1)
table.create(engine=db2)

This method will also produce correct keys, indexes, foreign keys. 此方法还将产生正确的键,索引,外键。 Once the needed tables are created, you can move the data by either select/insert if the tables are relatively small or use bcp utility to dump table to disk and then load it into the second database (much faster but more work to get it to work correctly) 一旦创建了所需的表,就可以通过选择/插入(如果表相对较小)来移动数据,或者使用bcp实用程序将表转储到磁盘上,然后将其加载到第二个数据库中(速度更快,但是要花很多时间才能将其存储到第二个数据库中)。正常工作)

If using select/insert then it is better to insert in batches of 500 records or so. 如果使用选择/插入,则最好分批插入500条左右的记录。

You can do something like this: 您可以执行以下操作:

tabs = pd.read_sql("SELECT table_name FROM INFORMATION_SCHEMA.TABLES", db1)

for tab in tabs['table_name']:
    pd.read_sql("select * from {}".format(tab), db1).to_sql(tab, db2, index=False)

But it might be be awfully slow. 但这可能会非常慢。 Use SQL Server tools to do this job. 使用SQL Server工具来完成这项工作。

Consider using sp_addlinkedserver procedure to link one SQL Server from another. 考虑使用sp_addlinkedserver过程将一个SQL Server链接到另一个。 After that you can execute: 之后,您可以执行:

SELECT * INTO server_name...table_name FROM table_name

for all tables from the db1 database. 用于db1数据库中的所有表。

PS this might be done in Python + SQLAlchemy as well... PS这也可以在Python + SQLAlchemy中完成...

MUPSAT 我尝试了您的技术来复制 sql server 数据库,但这仅适用于一个表,如果我想创建整个数据库的表,那么表的问题序列会发生,就像正在创建订单表但它引用另一个表是尚未创建它会给我一个错误,因此任何避免这种情况的技术都可以创建数据库的完整副本及其密钥和其他约束

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 sql-server 数据库到另一个数据库创建所有表及其键和其他约束的副本 - create a copy of all tables with its key and other constraints from sql-server database to another database 将数据库从一台服务器复制到另一台服务器,并在数据库事件上触发python脚本 - Copy database from one server to another server + trigger python script on a database event 如何使用Python imaplib将消息从一台imap服务器复制到另一台imap服务器? - How to copy a message from one imap server to another imap server using Python imaplib? 使用Python将数据从一个Oracle数据库复制到另一个数据库 - Copy data from one oracle database to another with Python PostgreSQL Python将表从一个数据库复制到另一个数据库 - PostgreSQL Python copy table from one database to another 从Python将整个SQL Server数据库复制到JSON - Copy whole SQL Server database into JSON from Python python - 如何使用插入命令将数据从一个SQL服务器传输到另一个SQL服务器? - How to transfer the data from one SQL server to another using insert into command using python? 如何使用python将公式从一个gsheet复制到另一个gsheet? - How to copy a formula from one gsheet to another using python? 如何使用 Python 将 zipfile 从一个文件夹复制到另一个文件夹 - How to copy zipfile from one folder to another folder using Python 有没有办法使用python将过滤器从一层复制到另一层 - is there a way to copy a filter from one layer to another layer using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM