BCP 資料的匯入和匯出

2021-07-31 21:15:30 字數 3854 閱讀 9749

bcp 命令的引數很多,使用 -h 檢視幫助資訊,注意:引數是區分大小寫的

使用bcp命令匯出和匯入資料常用的引數如下

bcp  | "query"}

資料檔案

[-c 字元型別]  | [-w 寬字元型別]

[-t 字段終止符]    [-r 行終止符]

[-i 輸入檔案]       [-o 輸出檔案]        

[-s 伺服器名稱]   [-u 使用者名稱]           [-p 密碼]

[-t 可信連線]      [-d 資料庫名稱]

[-k 保留null值]

-c 使用char型別做為儲存型別,沒有字首且以"\t"做為字段分割符,以"\n"做為行分割符。

-w 使用unicode字符集拷貝資料,在資料庫中,需要將table column設定為 nchar或nvarchar儲存型別。如果 -c 和 -w 同時指定,那麼 -w 將覆蓋 -c。

-t field_term 指定column分割符,預設是"\t"。

-r row_term 指定row分割符,預設是"\n"。

-s server_name[ \instance_name] 指定要連線的sql server伺服器的例項,如果未指定此選項,bcp連線本機的sql server預設例項。如果要連線某台機器上的預設例項,只需要指定機器名即可。

-u login_id 指定連線sql sever的使用者名稱。

-p password 指定連線sql server的使用者名稱密碼。

-t 指定bcp使用信任連線登入sql server。如果未指定-t,必須指定-u和-p。

-d 指定資料庫名稱

-k 指定空列使用null值插入,而不是這列的預設值。

一,使用bcp 將整個table中的資料匯出到txt或csv文件中

bcp db_study.dbo.sales out d:\test.txt -s . -u sa -p sa -t '\t'

-w bcp db_study.dbo.sales out d:\test.csv

-s . -u sa -p sa -t ','

-w

二,使用 query statement 將查詢結果匯出到txt 或 csv文件中

1,配置sql server,允許執行 xp_cmdshell 命令

--

允許配置高階選項

exec master.sys.sp_configure '

show advanced options

',1

--重新配置

reconfigure

--啟用xp_cmdshell

exec master.sys.sp_configure '

xp_cmdshell

',1

--重新配置

reconfigure

否則,sql server 會丟擲錯誤資訊

sql server 阻止了對元件「xp_cmdshell」的 過程「sys.xp_cmdshell」的訪問,因為此元件已作為此伺服器安全配置的一部分而被關閉。系統管理員可以通過使用 sp_configure 啟用「xp_cmdshell」。有關啟用「xp_cmdshell」的詳細資訊,請搜尋 sql server 聯機叢書中的「xp_cmdshell」。

啟用「xp_cmdshell」 被認為是不安全的,在使用完 「xp_cmdshell」 命令之後,使用以下script將其禁用。 

--

允許配置高階選項

exec master.sys.sp_configure '

show advanced options

',1

--重新配置

reconfigure

--禁用xp_cmdshell

exec master.sys.sp_configure '

xp_cmdshell

',0

--重新配置

reconfigure

2,使用  xp_cmdshell 命令執行bcp命令,將資料匯出

exec master..xp_cmdshell '

bcp "select [store] ,[item] ,[color] ,[quantity] from [db_study].[dbo].[inventory]" queryout d:\test.txt -s . -u sa -p sa -t "\t" -w

'exec master..xp_cmdshell '

bcp "select [store] ,[item] ,[color] ,[quantity] from [db_study].[dbo].[inventory]" queryout d:\test.csv -s . -u sa -p sa -t "," -w

'

3,使用  xp_cmdshell 命令,也可以將整個table的資料匯出

exec master..xp_cmdshell '

bcp [db_study].[dbo].[inventory] out d:\test.txt -s . -u sa -p sa -t "\t" -w

'exec master..xp_cmdshell '

bcp [db_study].[dbo].[inventory] out d:\test.csv -s . -u sa -p sa -t "," -w

'

三,將資料匯入到sql server

create

table

[dbo

].[inventory_loadin](

[store][n

varchar

](2) null

,

[item][

varchar

](20) null

,

[color][

varchar

](10) null

,

[quantity][

int]

null

)

1,使用bcp,將txt文件中的資料匯入到table [dbo].[inventory_loadin] 中

bcp [

db_study

].[dbo

].[inventory_loadin

]in d:\test.txt -s . -u sa -p sa -t "\t" -

w bcp

[db_study

].[dbo

].[inventory_loadin

]in d:\test.csv -s . -u sa -p sa -t "," -w

2,使用xp_cmdshell 命令執行bcp,將資料匯入到資料庫table中

exec master..xp_cmdshell '

bcp [db_study].[dbo].[inventory_loadin] in d:\test.txt -s . -u sa -p sa -t "\t" -w

'exec master..xp_cmdshell '

bcp [db_study].[dbo].[inventory_loadin] in d:\test.csv -s . -u sa -p sa -t "," -w

'

bcp匯入匯出

匯出 set cmd n bcp select from 庫.dbo.test1 n queryout d voc txt c u liyuepeng p liyuepeng exec master.xp cmdshell cmd set cmd n bcp 庫.dbo.test2 n in d v...

使用BCP匯出匯入資料

bcp 實用工具可以在 microsoft sql server 例項和使用者指定格式的資料檔案間大容量複製資料。使用 bcp 實用工具可以將大量新行匯入 sql server 表,或將表資料匯出到資料檔案。除非與 queryout 選項一起使用,否則使用該實用工具不需要了解 transact sq...

使用BCP匯出匯入資料

bcp實用工具可以在 microsoft sql server 例項和使用者指定格式的資料檔案間大容量複製資料。使用bcp實用工具可以將大量新行匯入 sql server 表,或將表資料匯出到資料檔案。除非與queryout選項一起使用,否則使用該實用工具不需要了解 transact sql 知識。...