     IF @outputColumnHeaders = 1
         BEGIN
              SET @tsqlCommand = 'EXECUTE xp_cmdshell ' +  '''bcp "SELECT ''''' + REPLACE(@Columns, ',',  @field_term) +
-                                '''''" queryout "' +  @path + @tableFullName + '_headers.txt' + '" -T -S ' +
+                                '''''" queryout "' + @path + @tableFullName + '_headers.' + @fileExtension + '" -T -S ' +
                                 @serverName + ' -c -' + @codePage + ' -t"' +  @field_term + '"' +
                                 ' -r"' +  @row_term + '"''' + @crlf;
-        
              IF @debug = 1 PRINT CAST(ISNULL('@tsqlCommand = {' + @crlf + @tsqlCommand + @crlf + '}',
                                       '@tsqlCommand = {Null}' + @crlf) AS TEXT);
              ELSE EXECUTE sp_executesql @tsqlCommand;
         
-             SET @cmdCommand = 'copy /b ' + @path + @tableFullName + '_headers.' + @fileExtension + ' + ' +
-                               @filePath + ' ' + @path + @tableFullName + '_headers.' + @fileExtension;
-        
-             IF @debug = 1 PRINT CAST(ISNULL('@cmdCommand = {' + @crlf + @cmdCommand + @crlf + '}', '@cmdCommand = {Null}' + @crlf) AS TEXT)
+             SET @cmdCommand = 'copy /b "' + @path + @tableFullName + '_headers.' + @fileExtension + '" + "' +
+                               @filePath + '" "' + @path + @tableFullName + '_headers.' + @fileExtension 
+                               + '" && ' + 'del "' + @filePath + '"'
+                               + ' && ' + 'ren "' + @path + @tableFullName + '_headers.'+ @fileExtension + '" ' + QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableName) + '.' + @fileExtension;
+             IF @debug = 1 PRINT CAST(ISNULL('@cmdCommand = {' + @crlf + @cmdCommand + @crlf + '}', '@cmdCommand = {Null}' + @crlf) AS TEXT);
              ELSE EXECUTE xp_cmdshell @cmdCommand;
         
-             SET @cmdCommand = 'del ' + @filePath;
-        
-             IF @debug = 1 PRINT CAST(ISNULL('@cmdCommand = {' + @crlf + @cmdCommand + @crlf + '}', '@cmdCommand = {Null}' + @crlf) AS TEXT)
-             ELSE EXECUTE xp_cmdshell @cmdCommand;
         END
+
+    IF @formatFile IN ('xml', 'fmt')
+    BEGIN
+        IF @savePhysicalOrder = 1 SET @tsqlCommand = 'EXECUTE xp_cmdshell ''bcp __full_table_name__ format nul -c -f "__path____filename__del.__format_file__ -t"|" -T''';
+        ELSE SET @tsqlCommand =
+'IF OBJECT_ID(''__databaseName__.__schemaTableName__'') IS NOT NULL DROP TABLE __schemaTableName__; SELECT __Columns__ INTO __schemaTableName__ FROM __full_table_name__ WHERE 1=2; EXECUTE xp_cmdshell ''bcp __databaseName__.__schemaTableName__ format nul -c -f "__path____filename__del.__format_file__ -t"|" -T'';DROP TABLE __schemaTableName__;'
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__databaseName__', QUOTENAME(@databaseName));
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__Columns__', @Columns);
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__schemaTableName__', QUOTENAME(@schemaName) + '.' + QUOTENAME(CASE WHEN @savePhysicalOrder = 1 THEN @tableName WHEN @savePhysicalOrder = 0 THEN 'tmp' + @tableName  END));
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__filename__', QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableName));
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__path__', @path);
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__format_file__', CASE WHEN @formatFile = 'xml' THEN 'xml" -x' WHEN @formatFile = 'fmt' THEN 'fmt"' END);
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__full_table_name__', @tableFullName);
+        IF @debug = 1 PRINT CAST(ISNULL('@tsqlCommand = {' + @crlf + @tsqlCommand + @crlf + '}', '@tsqlCommand = {Null}' + @crlf) AS TEXT);
+        ELSE EXECUTE sp_executesql @tsqlCommand; 
+    END
+
+    IF @formatFile in ('xml', 'fmt')
+    BEGIN
+
+        SET @tsqlCommand = N'select @OUT=CONVERT(VARCHAR(128), DATABASEPROPERTYEX(''__database_name__'', ''collation''));';
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__database_name__', @databaseName);
+        IF @debug = 1 PRINT CAST(ISNULL('@tsqlCommand = {' + @crlf + @tsqlCommand + @crlf + '}', '@tsqlCommand = {Null}' + @crlf) AS TEXT);
+        EXECUTE sp_executesql @tsqlCommand, @CollParmDefinition, @OUT=@current_collation OUTPUT;
+
+        SET @tsqlCommand = N'EXECUTE xp_cmdshell ''bcp "SELECT REPLACE(BulkColumn, ''''__replace__'''', ''''__collation__'''') AS BulkColumn FROM OPENROWSET(BULK ''''__path____filename__del.__formatFile__'''', __LOB__) as x" queryout "__path____filename__.__formatFile__" -c -T''';
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__LOB__', CASE WHEN @formatFile = 'xml' THEN 'SINGLE_NCLOB' WHEN @formatFile = 'fmt' THEN 'SINGLE_CLOB' END);
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__replace__', @current_collation); --'Cyrillic_General_CS_AS'
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__filename__', QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableName));
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__formatFile__', @formatFile);
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__path__', @path);
+        SET @tsqlCommand = REPLACE(@tsqlCommand, '__collation__', CASE WHEN @collation = '' AND @formatFile='fmt' THEN '""""' WHEN @collation = '' AND @formatFile = 'xml' THEN '' ELSE @collation END);
+        IF @debug = 1 PRINT CAST(ISNULL('@tsqlCommand = {' + @crlf + @tsqlCommand + @crlf + '}', '@tsqlCommand = {Null}' + @crlf) AS TEXT);
+        ELSE EXECUTE sp_executesql @tsqlCommand;
+
+        SET @cmdCommand = 'del "' + @path + QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableName) + 'del.' + @formatFile;
+        IF @debug = 1 PRINT CAST(ISNULL('@cmdCommand = {' + @crlf + @cmdCommand + @crlf + '}', '@cmdCommand = {Null}' + @crlf) AS TEXT);
+        ELSE EXECUTE xp_cmdshell @cmdCommand;
+    END
 
     IF @debug = 1 PRINT '--End Deubg*********/';
     ELSE SET NOCOUNT OFF;
     END TRY
 
     BEGIN CATCH
-        EXECUTE dbo.usp_LogError;
-        EXECUTE dbo.usp_PrintError;
+        --EXECUTE dbo.usp_LogError;
+        PRINT 'Error: '       + CONVERT(varchar(50), ERROR_NUMBER()) +
+              ', Severity: '  + CONVERT(varchar(5), ERROR_SEVERITY()) +
+              ', State: '     + CONVERT(varchar(5), ERROR_STATE()) +
+              ', Procedure: ' + ISNULL(ERROR_PROCEDURE(), '-') +
+              ', Line: '      + CONVERT(varchar(5), ERROR_LINE()) +
+              ', User name: ' + CONVERT(sysname, CURRENT_USER);
+        PRINT ERROR_MESSAGE();
     END CATCH
 END;
+GO

IF OBJECT_ID('dbo.usp_bcpTableUnload', 'P') IS NULL EXECUTE ('CREATE PROCEDURE dbo.usp_bcpTableUnload AS SELECT 1');
go

ALTER PROCEDURE dbo.usp_bcpTableUnload (
      @path                NVARCHAR(900)
    , @serverName          SYSNAME = @@SERVERNAME
    , @databaseName        SYSNAME
    , @schemaName          SYSNAME
    , @tableName           SYSNAME
    , @field_term          NVARCHAR(10)  = '|'
    , @row_term            NVARCHAR(10)  = '\n'
    , @fileExtension       NVARCHAR(10)  = 'txt'
    , @codePage            NVARCHAR(10)  = 'C1251'
    , @excludeColumns      NVARCHAR(MAX) = ''
    , @orderByColumns      NVARCHAR(MAX) = ''
    , @outputColumnHeaders BIT = 1
    , @debug               BIT = 0
)
AS
/*
bcp docs: https://msdn.microsoft.com/ru-ru/library/ms162802.aspx
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO

    DECLARE @tableFullName       SYSNAME       = QUOTENAME(@databaseName) + '.' + QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableName);
    DECLARE @object_id           INTEGER       = OBJECT_ID(@tableFullName);
    DECLARE @Columns             NVARCHAR(MAX) = '';
    DECLARE @collation_count     INTEGER;
    DECLARE @current_collation   VARCHAR(128)  = '';
    DECLARE @CollParmDefinition  NVARCHAR(500) = '@OUT VARCHAR(128) OUTPUT';
    DECLARE @filePath            NVARCHAR(900) = @path + @tableFullName + '.' + @fileExtension;
    DECLARE @crlf                NVARCHAR(10)  = CHAR(13);
    DECLARE @orderByColumns_term NVARCHAR(10)  = ',';
