我正在使用SQL Server 2008 Management Studio,并有一个表,我想迁移到不同的db服务器。

有任何选项导出数据作为插入到SQL脚本??


当前回答

下面是一个使用游标迭代源表创建数据迁移脚本的示例。

SET NOCOUNT ON;  
DECLARE @out nvarchar(max) = ''
DECLARE @row nvarchar(1024)
DECLARE @first int = 1

DECLARE cur CURSOR FOR 
    SELECT '(' + CONVERT(CHAR(1),[Stage]) + ',''' + [Label] + ''')'
    FROM CV_ORDER_STATUS
    ORDER BY [Stage]

PRINT 'SET IDENTITY_INSERT dbo.CV_ORDER_STATUS ON'
PRINT 'GO'

PRINT 'INSERT INTO dbo.CV_ORDER_STATUS ([Stage],[Label]) VALUES';

OPEN cur
FETCH NEXT FROM cur
    INTO @row

WHILE @@FETCH_STATUS = 0
BEGIN
    IF @first = 1
        SET @first = 0
    ELSE
        SET @out = @out + ',' + CHAR(13);

    SET @out = @out + @row

    FETCH NEXT FROM cur into @row
END

CLOSE cur
DEALLOCATE cur

PRINT @out

PRINT 'SET IDENTITY_INSERT dbo.CV_ORDER_STATUS OFF'
PRINT 'GO'

其他回答

你也可以查看SQL Server Management Studio 2008的“Data Scripter插件”:

http://www.mssql-vehicle-data.com/SSMS


它们的特点如下:

It was developed on SSMS 2008 and is not supported on the 2005 version at this time (soon!) Export data quickly to T-SQL for MSSQL and MySQL syntax CSV, TXT, XML are also supported! Harness the full potential, power, and speed that SQL has to offer. Don't wait for Access or Excel to do scripting work for you that could take several minutes to do -- let SQL Server do it for you and take all the guess work out of exporting your data! Customize your data output for rapid backups, DDL manipulation, and more... Change table names and database schemas to your needs, quickly and efficiently Export column names or simply generate data without the names. You can chose individual columns to script. You can chose sub-sets of data (WHERE clause). You can chose ordering of data (ORDER BY clause). Great backup utility for those grungy database debugging operations that require data manipulation. Don't lose data while experimenting. Manipulate data on the fly!

下面是一个使用游标迭代源表创建数据迁移脚本的示例。

SET NOCOUNT ON;  
DECLARE @out nvarchar(max) = ''
DECLARE @row nvarchar(1024)
DECLARE @first int = 1

DECLARE cur CURSOR FOR 
    SELECT '(' + CONVERT(CHAR(1),[Stage]) + ',''' + [Label] + ''')'
    FROM CV_ORDER_STATUS
    ORDER BY [Stage]

PRINT 'SET IDENTITY_INSERT dbo.CV_ORDER_STATUS ON'
PRINT 'GO'

PRINT 'INSERT INTO dbo.CV_ORDER_STATUS ([Stage],[Label]) VALUES';

OPEN cur
FETCH NEXT FROM cur
    INTO @row

WHILE @@FETCH_STATUS = 0
BEGIN
    IF @first = 1
        SET @first = 0
    ELSE
        SET @out = @out + ',' + CHAR(13);

    SET @out = @out + @row

    FETCH NEXT FROM cur into @row
END

CLOSE cur
DEALLOCATE cur

PRINT @out

PRINT 'SET IDENTITY_INSERT dbo.CV_ORDER_STATUS OFF'
PRINT 'GO'

对于那些寻找命令行版本的人来说,微软发布了mssql-scripter来做到这一点:

$ pip install mssql-scripter

# Generate DDL scripts for all database objects and DML scripts (INSERT statements)
# for all tables in the Adventureworks database and save the script files in
# the current directory
$ mssql-scripter -S localhost -d AdventureWorks -U sa --schema-and-data \
                 -f './' --file-per-object

dbatools。io是一个基于PowerShell的更加活跃的项目,它提供了Get-DbaDbTable和Export-DbaDbTableData cmdlet来实现这一点:

PS C:\> Get-DbaDbTable -SqlInstance sql2016 -Database MyDatabase \
           -Table 'dbo.Table1', 'dbo.Table2' | 
        Export-DbaDbTableData -Path C:\temp\export.sql

以上都很好,但如果你需要

使用连接从多个视图和表导出数据 为不同的rdbms创建插入语句 将数据从任意RDBMS迁移到任意RDBMS

那么下面的技巧是唯一的方法。

首先学习如何从源db命令行客户端创建spool文件或导出结果集。 第二,学习如何在目标db上执行sql语句。

最后,通过在源数据库上运行sql脚本,为目标数据库创建插入语句(以及任何其他语句)。 如。

SELECT '-- SET the correct schema' FROM dual;
SELECT 'USE test;' FROM dual;
SELECT '-- DROP TABLE IF EXISTS' FROM dual;
SELECT 'IF OBJECT_ID(''table3'', ''U'') IS NOT NULL DROP TABLE dbo.table3;' FROM dual;
SELECT '-- create the table' FROM dual;
SELECT 'CREATE TABLE table3 (column1 VARCHAR(10), column2 VARCHAR(10));' FROM dual;

SELECT 'INSERT INTO table3 (column1, column2) VALUES (''', table1.column1, ''',''', table2.column2, ''');' FROM table1 JOIN table2 ON table2.COLUMN1 = table1.COLUMN1;

上面的例子是为Oracle的db创建的,其中需要使用dual进行无表选择。

结果集将包含目标db的脚本。

SQl server Mng Studio 2016: