在SSMS中导出表并将其导入到其他服务器中

问题描述 投票:0回答:1

我们有运行带有大型SQL Server数据库的特定软件的客户端,他们使用SSMS对其进行管理(我们不希望他们在服务器上安装任何其他软件)。我们需要使用SSMS从服务器中取出许多表,以便它们可以保存它们(这必须是一个相对简单的过程,它将由IT经理而不是DBA或程序员来完成)并发送给我们(在USB驱动器上)。

所以我已经尝试过使用generate script选项,但是在测试中,我们确实得到了200GB .sql文件,然后我需要编辑第一行(USE [数据库])以指定其他数据库复制回(或每个客户端将具有相同的数据库名称并覆盖其他客户端的数据)。当然,在200GB文件中编辑行不是一件容易的事,因此我仍然必须使导入工作。

[我当时在想,如果我使用示例数据库中的generate脚本制作表等,然后使用SSMS中的导出功能将数据导出到CSV,但是,数据可能很干净,这很容易导致CSV问题。我当时在考虑使用平面文件而不是CSV,但是我担心它们可能会在编码等方面造成麻烦(并且我不确定与CSV相比,平面文件中的数据混乱程度如何。)

[我当时在想是否可以创建一个具有某种描述的SQL脚本来输出文件,但是它必须很简单,以便他们可以告诉代码中没有可疑对象,并且需要输出文件或文件集,但仍然存在如何保存而不出现数据损坏的相同问题。

有什么想法吗?我们使用的是Windows Server 2012 R2,数据可能来自不同版本的SQL Server,具体取决于该公司的最新更新。

sql-server import export ssms
1个回答
0
投票

除非有更好的答案,否则我将离开我们在这里所做的工作。

我们已经创建了一组指令和脚本,这些脚本和脚本将使客户端创建新数据库,然后使用该脚本将数据传输到新数据库,然后备份此新创建的数据库。

脚本(查询)有效地创建了一个循环,以遍历表并使用以下命令创建sql:

SET @sql = 'SELECT * INTO [' + @toDB + '].' + @currTable + ' FROM [' + @fromDB + '].' + @currTable

它将使用当前表名(@currTable),并将其从其主数据库(@fromDB)移至新创建的数据库(@toDB)。

这不是理想的选择,但目前看来,它是处理大量数据的最简单选择。最好的是,如果他们在选择要包括的表的备份时可以选择。

如果其他人需要执行类似操作,请参考以下脚本:

--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

[请注意,此脚本旨在提供给客户端,“请勿从此处向下编辑”是对它们的说明(您将需要编辑要复制的表名以及保存表总数的变量) 。

然后,我们向其发送了有关如何创建新数据库,运行此脚本然后备份新数据库等的一组说明。

© www.soinside.com 2019 - 2024. All rights reserved.