1

I have a SQL Server 2014 database from which I need to dump just the table data (no indexes, stored procedures, or anything else).

This dump needs to be imported into a Postgres 9.3 database "as-is".

What id the proper command line to create such a dump?

4

2 回答 2

2

我必须承认,这更像是一个笑话......您应该按照提示使用“导出”并将其写入某种 CSV。纯娱乐:

编辑:创建一个列列表以避免二进制列...

不能直接转换成XML RAW的列添加了“无效数据”:

DECLARE @Commands TABLE(ID INT IDENTITY,cmd NVARCHAR(MAX));

INSERT INTO @Commands(cmd)
SELECT '(SELECT TOP 3 ' 
                        + STUFF(
                            (
                                SELECT ',' + QUOTENAME(COLUMN_NAME)
                                FROM INFORMATION_SCHEMA.COLUMNS AS c 
                                WHERE c.TABLE_CATALOG=t.TABLE_CATALOG AND c.TABLE_SCHEMA=t.TABLE_SCHEMA AND c.TABLE_NAME=t.TABLE_NAME
                                  AND c.DATA_TYPE NOT IN('image','text') AND c.DATA_TYPE NOT LIKE '%BINARY%'
                                FOR XML PATH('')
                            ),1,1,''
                          )
                        + 
                            (
                                SELECT ',''invalid data'' AS ' + QUOTENAME(COLUMN_NAME)
                                FROM INFORMATION_SCHEMA.COLUMNS AS c 
                                WHERE c.TABLE_CATALOG=t.TABLE_CATALOG AND c.TABLE_SCHEMA=t.TABLE_SCHEMA AND c.TABLE_NAME=t.TABLE_NAME
                                  AND (c.DATA_TYPE IN('image','text') OR c.DATA_TYPE LIKE '%BINARY%')
                                FOR XML PATH('')
                            )

                        + ' FROM '  + QUOTENAME(t.TABLE_CATALOG) + '.' + QUOTENAME(t.TABLE_SCHEMA) + '.'
       + QUOTENAME(t.TABLE_NAME) + ' FOR XML RAW,TYPE) AS ' + QUOTENAME(t.TABLE_CATALOG + '_' + t.TABLE_SCHEMA + '_' + t.TABLE_NAME)  
FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_TYPE='BASE TABLE';

DECLARE @finalCommand NVARCHAR(MAX)=
(
    SELECT 'SELECT '
          +'(SELECT TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES FOR XML RAW,TYPE) AS ListOfTables'
          + (
                SELECT ',' + cmd
                FROM @Commands
                ORDER BY ID
                FOR XML PATH('')
            )
          + ' FOR XML PATH(''AllTables'')'
);

EXEC( @finalCommand);
于 2016-09-15T19:31:43.497 回答
0

微软最近宣布了一个新的命令行工具 mssql-scripter(它是开源和多操作系统),它允许您为数据库/数据库对象生成 T-SQL 脚本作为 .sql 文件。公告在这里

启动脚本编写器后,您将需要运行类似于以下内容的命令:

$ mssql-scripter -S serverName -U userName -d databaseName --data-only

更多详细信息请参见 GitHub 页面使用指南:https ://github.com/Microsoft/sql-xplat-cli/blob/dev/doc/usage_guide.md

于 2017-05-25T16:49:47.020 回答