Thng tin: Th?ng g?p cu h?i BCP ti?n ch trong SQL

D?ch tiu ? D?ch tiu ?
ID c?a bi: 67409 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny


Chng tr?nh sao chp hng lo?t (BCP) l m?t ti?n ch cho vi?c sao chp m?t l?ng l?n d? li?u vo ho?c ra kh?i my ch? SQL. BCP ch? y?u ?c s? d?ng ? sao chp d? li?u t? m?t SQL Server ? s? d?ng v?i cc chng tr?nh khc (b?ng tnh), t?i ln d? li?u t?p tin t? c s? d? li?u khc nhau, v sao chp thng tin t? m?t SQL My ch? ? khc SQL Server. D?i y danh sch cc th?ng g?p cu h?i v v?n ? m ng?i dng kinh nghi?m trong khi s? d?ng BCP Ti?n ch.


B?ng cch s? d?ng chng tr?nh sao chp hng lo?t

Q. Could you please explain how to use the BCP format file?

A. The BCP format file provides an alternative way to use the BCP
   utility and adds flexibility to the program. The following example
   of a format file illustrates the use of BCP on an ASCII data file

     BCP Format File
     1    sybchar 0      12        ""        1    f_name
     2    sybchar 0      15        ""        2    l_name
     3    sybchar 0      15        ","       3    city
     4    sybchar 0      2         "\r\n"         4    state


     Row  Description
     ---  -----------
      1   Indicates the version number of BCP.
      2   Represents the number of fields in the data file.

  Column  Description
  ------  -----------
      1   Designates the field number of the data file.
      2   Indicates the data type that is stored in the particular
          field of the data file. For ASCII data files, use "sybchar";
          and for native format data files, use the default data
      3   Indicates the prefix length, which is used to provide the
          most compact file storage. For ASCII data files, use 0.
      4   Represents the length of the particular data file field.
      5   Represents the field terminator. Common field terminators
          are commas and end of line characters (\r\n).
      6   Indicates the column number of the table in the SQL Server
      7   Gives the name of the field taken from the SQL table. It is
          not necessary to use the actual name of the field. However,
          the only condition is that it cannot be blank.

Q. What is the difference between data in ASCII format and data in
   native format, and how does the data format relate to BCP?

A. The information in BCP data files can be stored in two ways: ASCII
   and native format.

   In ASCII data files, the information is stored as ASCII characters.
   This format is useful if the data will be used with another
   program, such as a spreadsheet, or if the data is to be uploaded to
   SQL Server from some other database. The BCP format file described
   in question 1 is an example of a format file that could be used to
   BCP ASCII data out of or into SQL Server. Note that all data types
   are defined to be "sybchar" and have a prefix length of 0. The SQL
   Server engine performs all the data type conversions.

   The native format file contains the data's "native" data types as
   defined in the SQL Server database. Native format proves to be most
   useful when information is to be BCP'd out of one SQL Server and
   into a different SQL Server. The use of the native format saves
   time by eliminating the unnecessary conversion of data types into
   and out of character format. The following is an example of a
   native format BCP format file:

     BCP Format File
     1    sybchar 1      512       ""        1    discnttype
     2    sybchar 1      512       ""        2    stor_id
     3    sybint2 1      2         ""        3    lowqty
     4    sybint2 1      2         ""        4    highqty
     5    sybflt8 0      8         ""        5    discount

   This particular format file was created by BCP'ing data out of the
   "discount" table from the "pubs" database and taking all default
   values for the fields.

Q. What is the proper way to use the field length column and the
   delimiter column? How can they work with or against each other?

A. Data can be field delimited either by a character set or by a
   specified fixed length. It can also be delimited using a
   combination of both character set and fixed length. However,
   caution must be taken. The BCP utility will take the first form of
   the delimiter it finds. For example, if the field is comma
   delimited and all elements in the field have at least 10
   characters, and the length is set at 8, BCP will copy only the
   first 8 characters of all elements in that field. This mistake will
   most likely result in an error message from BCP. It is easiest to
   use the default option of fixed-length terminators for native
   format files.

Q. What is the best way to create a format file to BCP my ASCII data
   into SQL Server?

A. To create a format file that is easily modified, start the BCP
   utility by using the following command line:

      bcp <db_name>.<owner>.<table> out <data_file> /S<server> /Usa /P<password>

   When you start the program, a series of questions is asked. Choose
   all the default options. When prompted to save the information in a
   BCP format file, press Y, then ENTER. Then edit the format file to
   have data types of "sybchar" and a prefix length of 0 (zero), and
   enter the proper length and/or field terminators.

   After this process is completed, run the BCP utility with the
   /f <bcp.fmt> option. This option tells BCP to use the specified BCP
   format file.

Q. Now that I know the structure and usage of the BCP format file, how
   can I use it to ignore extra columns in my ASCII file?

A. If you have a data file that contains more fields than the table on
   SQL Server, you can redirect these unneeded fields to a null field,
   as in the following example, where the user wants to copy in the
   fields "f_name", "l_name", and "state", but does not want to make
   use of the "city" field that exists in the data file. The following
   is an example of both the data file and the BCP format file:

     ASCII Data File
     Howard       Jones   San Jose,CA
     William      Smith   Seattle,WA
     Anthony      Henry   Portland,OR

     BCP Format File
     1    sybchar 0      10        ""        2    f_name
     2    sybchar 0      10        ""        1    l_name
     3    sybchar 0      15        ","       0    city
     4    sybchar 0      2         "\r\n"         3    state

   Please note that on the fifth line of the BCP format file, the SQL
   Server table field designation (column 6) is 0 and the table field
   name (column 7) still has some form of an entry. The use of 0 in
   column 6 will allow any information between column 20 and the comma
   in the data file to be placed in the null field device (the city
   fields). The use of column 7 is to avoid a BCP syntax error and is
   useful in self documentation.

Q. What are the most common problems seen when using BCP? What are the
   symptoms? And how do you solve the problems?

A. The most common problems users have when using BCP fall into the
   following categories:

   *  Trying to load native format.
      Users often try to load an ASCII file using the SQL Server
      native format. This results in all the hexadecimal values of the
      ASCII file being misinterpreted. An "unexpected end of file"
      error message is very common in this instance. The proper way to
      load the ASCII file is to represent the fields as character
      strings and to let SQL Server perform the data conversion.

   *  Using the default size of the data type.
      Using the default size can sometimes lead to the same error as
      above ("unexpected end of file"). This is most common with the
      SQL Server money and date data types. For example, if the user
      does not insert the entire date string, but only a fraction of
      the entire date (that is, mm/dd/yy with no time), the default
      size option reads 25 characters, the length of the date type.
      The solution is to use delimiters or a user-defined, fixed-
      length ASCII file.

   *  Hidden characters in the data file.
      Often a hidden character in an ASCII data file can cause
      problems when the BCP command is used. This problem usually
      masks itself in an "unexpected null found" error. This character
      usually can be found at the bottom of the file. Many utilities
      display hidden characters; finding and removing this hidden
      character should resolve the problem.

   *  Using spaces for delimiters in a data file.
      Using the BCP command with the format file on a data file
      delimited with spaces returns the following error message "dblib
      error: column number out of range." To avoid this problem, run
      BCP without a format file by using the -t option on the command
      line. For example:

       bcp dbase..table out file.out -SServer -Usa -P -r\r\n -c -t" "

Khi ng cch hi?u, BCP c th? ?c s? d?ng ? chuy?n m?t l?ng l?n d? li?u vo ho?c ra kh?i my ch? SQL. B?ng cch s? d?ng ?ng d?ng lu ? i?u ny v Microsoft SQL Server "Qu?n tr? h? th?ng h?ng d?n" s? gip b?n s? d?ng BCP thnh cng.

Thu?c tnh

ID c?a bi: 67409 - L?n xem xt sau cng: 17 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft SQL Server 4.21a Standard Edition
T? kha:
kbinfo kbmt KB67409 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:67409
Khc t Ni dung trong C s Kin thc Khng con c h tr
Bi vi?t ny ni v? cc s?n ph?m m Microsoft khng c?n h? tr? n?a. Do , bi vi?t ny ?c cung c?p "nguyn b?n" v s? khng ?c c?p nh?t.

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from