How to dump an entire SQL Server 2014 database into a file, to be imported into a Postgres database?

后端 未结 2 382
情深已故
情深已故 2021-01-24 11:29

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

相关标签:
2条回答
  • 2021-01-24 11:55

    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);
    
    0 讨论(0)
  • 2021-01-24 12:02

    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

    0 讨论(0)
提交回复
热议问题