Share via


bcp_writefmt

Se aplica a:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)

Crea un archivo de formato que contiene una descripción del formato del archivo de datos de la copia masiva actual.

Sintaxis

  
RETCODE bcp_writefmt (  
        HDBC hdbc,  
        LPCTSTR szFormatFile);  

Argumentos

hdbc
Es el identificador de la conexión ODBC habilitada para la copia masiva.

szFormatFile
Es la ruta de acceso y nombre del archivo de usuario para recibir valores de formato para el archivo de datos.

Devoluciones

SUCCEED o FAIL.

Observaciones

El archivo de formato especifica el formato de los datos de un archivo de datos creado mediante copia masiva. Las llamadas a bcp_columns y bcp_colfmt definen el formato del archivo de datos. bcp_writefmt guarda esta definición en el archivo al que se hace referencia en szFormatFile. Para obtener más información, vea bcp_init.

Para obtener más información sobre la estructura de los archivos de formato de datos bcp, vea Import and Export Bulk Data by Using the bcp Utility (SQL Server)).

Para cargar un archivo de formato guardado, use bcp_readfmt.

Nota

El archivo de formato generado por bcp_writefmt solo es compatible con las versiones de la utilidad bcp distribuidas con SQL Server versión 7.0 y posteriores.

Ejemplo

// Variables like henv not specified.  
HDBC      hdbc;  
DBINT      nRowsProcessed;  
  
// Application initiation, get an ODBC environment handle, allocate the  
// hdbc, and so on.  
...   
  
// Enable bulk copy prior to connecting on allocated hdbc.  
SQLSetConnectAttr(hdbc, SQL_COPT_SS_BCP, (SQLPOINTER) SQL_BCP_ON,  
   SQL_IS_INTEGER);  
  
// Connect to the data source, return on error.  
if (!SQL_SUCCEEDED(SQLConnect(hdbc, _T("myDSN"), SQL_NTS,  
   _T("myUser"), SQL_NTS, _T("myPwd"), SQL_NTS)))  
   {  
   // Raise error and return.  
   return;  
   }  
  
// Initialize bulk copy.   
if (bcp_init(hdbc, _T("myTable"), _T("myData.csv"),  
   _T("myErrors"),    DB_OUT) == FAIL)  
   {  
   // Raise error and return.  
   return;  
   }  
  
if (bcp_columns(hdbc, 3) == FAIL)  
   {  
   // Raise error and return.  
   return;  
   }  
  
bcp_colfmt(hdbc, 1, SQLCHARACTER, 0, SQL_VARLEN_DATA, '\t', 1, 1);  
bcp_colfmt(hdbc, 2, SQLCHARACTER, 0, SQL_VARLEN_DATA, '\t', 1, 2);  
bcp_colfmt(hdbc, 3, SQLCHARACTER, 0, SQL_VARLEN_DATA, '\t', 1, 3);  
  
if (bcp_writefmt(hdbc, _T("myFmtFile.fmt")) == FAIL)  
   {  
   // Raise error and return.  
   return;  
   }  
  
if (bcp_exec(hdbc, &nRowsProcessed) == SUCCEED)  
   {  
   printf_s("%ld rows copied from SQL Server\n", nRowsProcessed);  
   }  
  
// Carry on.  

Consulte también

Bulk Copy Functions