SQL Server bcp 及 NullIf 的使用

来源:互联网 发布:防伪底纹设计软件 编辑:程序博客网 时间:2024/05/16 08:42

bcp

The bcp utility bulk copies data between an instance of Microsoft SQL Server 2005 and a data file in a user-specified format. Thebcp utility can be used to import large numbers of new rows into SQL Server tables or to export data out of tables into data files. Except when used with thequeryout option, the utility requires no knowledge of Transact-SQL. To import data into a table, you must either use a format file created for that table or understand the structure of the table and the types of data that are valid for its columns.  

Syntax

 
bcp {[[database_name.][owner].]{table_name | view_name} | "query"}    {in | out | queryout | format} data_file    [-mmax_errors] [-fformat_file] [-x] [-eerr_file]    [-Ffirst_row] [-Llast_row] [-bbatch_size]    [-n] [-c] [-w] [-N] [-V (60 | 65 | 70 | 80)] [-6]     [-q] [-C { ACP | OEM | RAW | code_page } ] [-tfield_term]     [-rrow_term] [-iinput_file] [-ooutput_file] [-apacket_size]    [-Sserver_name[\instance_name]] [-Ulogin_id] [-Ppassword]    [-T] [-v] [-R] [-k] [-E] [-h"hint [,...n]"]


ULLIF (Transact-SQL)

Returns a null value if the two specified expressions are equivalent.

Syntax

 
NULLIF ( expression , expression )


注意事项:


对于 bcp ,需要注意:

1, 其中的SQL Script 语句不可换行,必须在一行内写完,否则会无法执行。

2, 其参数中 -c 及 -w 会导致不同的txt 文件格式,ANSI 和 unico 格式


为何要将此两项放在一块说明,因近期发现并解决了一个问题,原因正是出在bcp 输出txt 文件时,最终必须使用 NULLIF 才能正常将相关Column Value 输出为一个可以被其它系统功能识别的txt 文件。否则会将显示为一个 Null 值,(字段间使用 tab 分隔) 导致其它功能导入此txt 文件时,获取空字段值时取得的值却显示为#符号,使用Notepad+ 检查发现此符号显示为Null。

使用NullIf 方式处理,如 NullIf(column1, ' ') 即判断字段值是否为一个空白字符串,如是则显示为Null ,否则显示为正常的 Column Value,如此处理后,竟然结果OK了,Notepad+ 中不再显示为Null,在其它功能导放此txt 文件时也不再显示为#。


很奇怪但也算了解了NULLIF 的特别使用方式。






0 0
原创粉丝点击