在 SSMS 中导出表并将它们导入到不同的服务器上
Export tables in SSMS and import them onto a different server
我们的客户 运行 具有大型 SQL 服务器数据库的特定软件,他们使用 SSMS 来管理它(我们不想要求他们安装任何其他软件在他们的服务器上)。我们需要使用 SSMS 从服务器中获取一些表,以便他们可以保存它们(这必须是一个相对容易的过程,它将由 IT 经理而不是 DBA 或程序员来执行任务)并将它们发送给我们(在 USB 驱动器上)。
所以我尝试了生成脚本选项,但在测试中我最终得到了一个 200GB .sql
文件,然后我需要编辑第一行(USE [database])指定一个不同的数据库来复制回(或者每个客户端将具有相同的数据库名称并覆盖来自其他客户端的数据)。当然,在一个200GB的文件中编辑一行并不是一件容易的事,那我还得导入才能工作。
我在想也许如果我使用示例数据库中的生成脚本在我这边制作表格等,然后使用 SSMS 中的导出功能将数据导出到 CSV,但是,数据可能是任何东西都不干净,这很容易导致 CSV 出现问题。我在考虑一个平面文件而不是 CSV,但我担心他们可能会用编码等填充一些东西(而且我不确定与 CSV 相比,平面文件中的数据有多混乱)。
我在想也许我可以创建一个 SQL 一些描述的脚本来输出一个文件,但它必须是简单的东西,这样他们才能看出代码中没有任何可疑之处,并且需要输出一个文件或一组文件,但仍然会有同样的问题,即如何在不损坏数据的情况下进行保存。
有什么想法吗?我们在 Windows Server 2012 R2 上,数据可能来自 SQL 服务器的各种版本,具体取决于该公司最近更新的时间。
在没有更好的答案之前,我将把我们所做的留在此处。
我们已经创建了一组指令和一个脚本,可以让客户端创建一个新数据库,然后使用脚本将数据传输到新数据库,然后备份这个新创建的数据库。
脚本(查询)有效地创建了一个循环来遍历 tables 并创建 sql 具有:
SET @sql = 'SELECT * INTO [' + @toDB + '].' + @currTable + ' FROM [' + @fromDB + '].' + @currTable
采用当前 table 名称 (@currTable) 并将其从主数据库 (@fromDB) 移至新创建的数据库 (@toDB)。
这并不理想,但目前看来是处理大量数据的最简单选择。如果他们在进行备份时可以选择要包含哪些 table,那就太好了。
供参考,如果其他人需要做这样的事情,这里是脚本:
--before you run this script, check that the 2 variables at the top are set correctly
--the @toDB variable should be a database you have just created to temporarily store exported data
DECLARE @fromDB VARCHAR(max) = 'main_database' --this should be set to the name of the database you are copying from
DECLARE @toDB VARCHAR(max) = 'main_database_export' --this should be set to the name of the database you are copying to (the temporary one)
/* ------------------------------------------
---------Do not edit from here down---------
------------------------------------------- */
--declare variables to be used in different parts of the script
DECLARE @sql VARCHAR(max)
DECLARE @currPos INT = 1
DECLARE @currTable VARCHAR(max)
DECLARE @tableNames TABLE(id INT, name varchar(max))
--create a list of files that we want top copy to the new database, the id must be sequential and start at 1)
INSERT INTO @tableNames VALUES
(1, '[dbo].[table1]'),
(2, '[dbo].[table2]'),
(3, '[dbo].[table3]'),
(4, '[dbo].[table4]')
DECLARE @totalTables INT = 4 --this should always be the number of the last table to be copied, if you add more or take any away, update this
--loop through the tables and copy them across
WHILE (@currPos <= @totalTables)
BEGIN
--get the table name of the table we are up to
SELECT @currTable = name FROM @tableNames WHERE id = @currPos
--create the sql that will copy from the old table into the new table (including the table structure), this table must not exist yet
SET @sql = 'SELECT * INTO [' + @toDB + '].' + @currTable + ' FROM [' + @fromDB + '].' + @currTable
--run the sql statement we just created, this will create the table and copy the content (and leave a message to say how many rows were copied)
EXECUTE (@sql)
--set the counter up one so we move onto the next table
SET @currPos = @currPos+1
--output the name of the table that was just processed (note that no messages will show until the entire script finishes)
PRINT @currTable + ' Copied.'
END
请注意,此脚本旨在提供给客户,"Do not edit from here down" 是给他们的说明(您需要编辑要复制的 table 名称和保存总数的变量table 的数量)。
然后我们会发送一组关于如何创建新数据库的说明,运行这个脚本然后备份新数据库等等。
我们的客户 运行 具有大型 SQL 服务器数据库的特定软件,他们使用 SSMS 来管理它(我们不想要求他们安装任何其他软件在他们的服务器上)。我们需要使用 SSMS 从服务器中获取一些表,以便他们可以保存它们(这必须是一个相对容易的过程,它将由 IT 经理而不是 DBA 或程序员来执行任务)并将它们发送给我们(在 USB 驱动器上)。
所以我尝试了生成脚本选项,但在测试中我最终得到了一个 200GB .sql
文件,然后我需要编辑第一行(USE [database])指定一个不同的数据库来复制回(或者每个客户端将具有相同的数据库名称并覆盖来自其他客户端的数据)。当然,在一个200GB的文件中编辑一行并不是一件容易的事,那我还得导入才能工作。
我在想也许如果我使用示例数据库中的生成脚本在我这边制作表格等,然后使用 SSMS 中的导出功能将数据导出到 CSV,但是,数据可能是任何东西都不干净,这很容易导致 CSV 出现问题。我在考虑一个平面文件而不是 CSV,但我担心他们可能会用编码等填充一些东西(而且我不确定与 CSV 相比,平面文件中的数据有多混乱)。
我在想也许我可以创建一个 SQL 一些描述的脚本来输出一个文件,但它必须是简单的东西,这样他们才能看出代码中没有任何可疑之处,并且需要输出一个文件或一组文件,但仍然会有同样的问题,即如何在不损坏数据的情况下进行保存。
有什么想法吗?我们在 Windows Server 2012 R2 上,数据可能来自 SQL 服务器的各种版本,具体取决于该公司最近更新的时间。
在没有更好的答案之前,我将把我们所做的留在此处。
我们已经创建了一组指令和一个脚本,可以让客户端创建一个新数据库,然后使用脚本将数据传输到新数据库,然后备份这个新创建的数据库。
脚本(查询)有效地创建了一个循环来遍历 tables 并创建 sql 具有:
SET @sql = 'SELECT * INTO [' + @toDB + '].' + @currTable + ' FROM [' + @fromDB + '].' + @currTable
采用当前 table 名称 (@currTable) 并将其从主数据库 (@fromDB) 移至新创建的数据库 (@toDB)。
这并不理想,但目前看来是处理大量数据的最简单选择。如果他们在进行备份时可以选择要包含哪些 table,那就太好了。
供参考,如果其他人需要做这样的事情,这里是脚本:
--before you run this script, check that the 2 variables at the top are set correctly
--the @toDB variable should be a database you have just created to temporarily store exported data
DECLARE @fromDB VARCHAR(max) = 'main_database' --this should be set to the name of the database you are copying from
DECLARE @toDB VARCHAR(max) = 'main_database_export' --this should be set to the name of the database you are copying to (the temporary one)
/* ------------------------------------------
---------Do not edit from here down---------
------------------------------------------- */
--declare variables to be used in different parts of the script
DECLARE @sql VARCHAR(max)
DECLARE @currPos INT = 1
DECLARE @currTable VARCHAR(max)
DECLARE @tableNames TABLE(id INT, name varchar(max))
--create a list of files that we want top copy to the new database, the id must be sequential and start at 1)
INSERT INTO @tableNames VALUES
(1, '[dbo].[table1]'),
(2, '[dbo].[table2]'),
(3, '[dbo].[table3]'),
(4, '[dbo].[table4]')
DECLARE @totalTables INT = 4 --this should always be the number of the last table to be copied, if you add more or take any away, update this
--loop through the tables and copy them across
WHILE (@currPos <= @totalTables)
BEGIN
--get the table name of the table we are up to
SELECT @currTable = name FROM @tableNames WHERE id = @currPos
--create the sql that will copy from the old table into the new table (including the table structure), this table must not exist yet
SET @sql = 'SELECT * INTO [' + @toDB + '].' + @currTable + ' FROM [' + @fromDB + '].' + @currTable
--run the sql statement we just created, this will create the table and copy the content (and leave a message to say how many rows were copied)
EXECUTE (@sql)
--set the counter up one so we move onto the next table
SET @currPos = @currPos+1
--output the name of the table that was just processed (note that no messages will show until the entire script finishes)
PRINT @currTable + ' Copied.'
END
请注意,此脚本旨在提供给客户,"Do not edit from here down" 是给他们的说明(您需要编辑要复制的 table 名称和保存总数的变量table 的数量)。
然后我们会发送一组关于如何创建新数据库的说明,运行这个脚本然后备份新数据库等等。