 IF OBJECT_ID('dbo.usp_bcpTableUnload', 'P') IS NULL EXECUTE ('CREATE PROCEDURE dbo.usp_bcpTableUnload AS SELECT 1');
-go
-
-ALTER PROCEDURE dbo.usp_bcpTableUnload (
+GO
+
+ALTER PROCEDURE [dbo].[usp_bcpTableUnload](
       @path                NVARCHAR(900)
-    , @serverName          SYSNAME = @@SERVERNAME
+    , @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
+    , @field_term          NVARCHAR(10)  = N'|'
+    , @row_term            NVARCHAR(10)  = N'\n'
+    , @fileExtension       NVARCHAR(10)  = N'txt'
+    , @codePage            NVARCHAR(10)  = N'C65001'
+    , @excludeColumns      NVARCHAR(MAX) = N''
+    , @orderByColumns      NVARCHAR(MAX) = N''
+    , @outputColumnHeaders BIT           = 1
+    , @savePhysicalOrder   BIT           = 1     -- 0 - Alphabet column order type; 1 - physical column order type
+    , @formatFile          VARCHAR(4)    = 'xml' -- formatfile = @schemaName.@tableName.fmt, -x formatfile = @schemaName.@tableName.xml, blank string without generation format file
+    , @collation           VARCHAR(128)  = ''    -- blank for keep blank value in formatfile (formatfile value 1 needed)
+    , @debug               BIT           = 0
 )
 AS
 /*
-bcp docs: https://msdn.microsoft.com/ru-ru/library/ms162802.aspx
+bcp docs: https://docs.microsoft.com/en-us/sql/tools/bcp-utility
 -- To allow advanced options to be changed.
 EXEC sp_configure 'show advanced options', 1;
 GO


    IF @debug = 1 PRINT ISNULL('/******* Start Debug' + @crlf + '@tableFullName = {' + CAST(@tableFullName AS NVARCHAR(MAX)) + '}', '@tableFullName = {Null}');
    IF @debug = 1 PRINT ISNULL('@object_id = {' + CAST(@object_id AS NVARCHAR(MAX)) + '}', '@object_id = {Null}');

    IF @object_id IS NULL THROW 50000, @TROW50000, 1
    IF @orderByColumns <> ''

                       N'SELECT @ColumnsOUT = @ColumnsOUT + QUOTENAME(Name) + '','''                            + @crlf +
                       N'FROM sys.columns sac '                                                                 + @crlf +
                       N'WHERE sac.object_id = @object_idIN'                                                    + @crlf +
                       N'      AND QUOTENAME(Name) NOT IN (''' + REPLACE(@excludeColumns, ',', ''',''') + ''')
                       __row_order__;';

    IF @savePhysicalOrder = 1 SET @tsqlCommand = REPLACE(@tsqlCommand, '__row_order__', '');
    ELSE SET @tsqlCommand = REPLACE(@tsqlCommand, '__row_order__', 'ORDER BY Name');

    IF @debug = 1 PRINT ISNULL(N'@tsqlCommand = {' + @crlf + @tsqlCommand + @crlf + N'}', N'@tsqlCommand = {Null}');
    EXECUTE sp_executesql @tsqlCommand, @ParmDefinition, @object_idIN = @object_id, @ColumnsOUT = @Columns OUTPUT SELECT @Columns;

    IF @debug = 1 PRINT ISNULL('@Columns = {' + @crlf + @Columns + @crlf + '}', '@Columns = {Null}');
