How to dump an entire SQL Server 2014 database int

2020-04-21 04:40发布

问题:

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?

回答1:

I must admit, this is more sort of a joke... You should rather follow the hint to use "Export" and write this to some kind of CSV. Just for fun:

EDIT: create a column list to avoid binary columns...

columns, which are not directly convertible into XML RAW are added with "invalid data":

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


回答2:

Microsoft recently announced a new command line tool mssql-scripter (it's open source and multi-OS) that allows you to generate T-SQL scripts for databases/database objects as a .sql file. The announcement is here.

Once launching the scripter you'll want to run a command similar to the following:

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

More details are on the GitHub page usage guide: https://github.com/Microsoft/sql-xplat-cli/blob/dev/doc/usage_guide.md