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

后端 未结 2 381
情深已故
情深已故 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);
    

提交回复
热议问题