BCP 数据的导入和导出
BCP 命令的參數(shù)很多,使用 -h 查看幫助信息,注意:參數(shù)是區(qū)分大小寫的
使用BCP命令導(dǎo)出和導(dǎo)入數(shù)據(jù)常用的參數(shù)如下
bcp{[[database_name.][schema_name]].{table_name | view_name} | "query"}
{in|out|queryout} 數(shù)據(jù)文件
[-c 字符類型] | [-w 寬字符類型]
[-t 字段終止符] [-r 行終止符]
[-i 輸入文件] [-o 輸出文件]
[-S 服務(wù)器名稱] [-U 用戶名] [-P 密碼]
[-T 可信連接] [-d 數(shù)據(jù)庫名稱]
[-k 保留NULL值]
-c 使用char類型做為存儲類型,沒有前綴且以" "做為字段分割符,以"
"做為行分割符。
-w 使用Unicode字符集拷貝數(shù)據(jù),在數(shù)據(jù)庫中,需要將Table Column設(shè)置為 nchar或nvarchar存儲類型。如果 -c 和 -w 同時指定,那么 -w 將覆蓋 -c。
-t field_term 指定column分割符,默認(rèn)是" "。
-r row_term 指定row分割符,默認(rèn)是"
"。
-S server_name[ instance_name] 指定要連接的SQL Server服務(wù)器的實例,如果未指定此選項,BCP連接本機的SQL Server默認(rèn)實例。如果要連接某臺機器上的默認(rèn)實例,只需要指定機器名即可。
-U login_id 指定連接SQL Sever的用戶名。
-P password 指定連接SQL Server的用戶名密碼。
-T 指定BCP使用信任連接登錄SQL Server。如果未指定-T,必須指定-U和-P。
-d 指定數(shù)據(jù)庫名稱
-k 指定空列使用null值插入,而不是這列的默認(rèn)值。
一,使用bcp 將整個table中的數(shù)據(jù)導(dǎo)出到txt或csv文檔中
bcp db_study.dbo.sales out D: est.txt -S . -U sa -P sa -t ' ' -w
bcp db_study.dbo.sales out D: est.csv -S . -U sa -P sa -t ',' -w
二,使用 query statement 將查詢結(jié)果導(dǎo)出到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”的訪問,因為此組件已作為此服務(wù)器安全配置的一部分而被關(guān)閉。系統(tǒng)管理員可以通過使用 sp_configure 啟用“xp_cmdshell”。有關(guān)啟用“xp_cmdshell”的詳細(xì)信息,請搜索 SQL Server 聯(lián)機叢書中的“xp_cmdshell”。
啟用“xp_cmdshell” 被認(rèn)為是不安全的,在使用完 “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命令,將數(shù)據(jù)導(dǎo)出
EXEC master..xp_cmdshell 'bcp "SELECT [Store] ,[Item] ,[Color] ,[Quantity] FROM [db_study].[dbo].[Inventory]" queryout D: est.txt -S . -U sa -P sa -t " " -w ' EXEC master..xp_cmdshell 'bcp "SELECT [Store] ,[Item] ,[Color] ,[Quantity] FROM [db_study].[dbo].[Inventory]" queryout D: est.csv -S . -U sa -P sa -t "," -w '
3,使用 xp_cmdshell 命令,也可以將整個Table的數(shù)據(jù)導(dǎo)出
EXEC master..xp_cmdshell 'bcp [db_study].[dbo].[Inventory] out D: est.txt -S . -U sa -P sa -t " " -w ' EXEC master..xp_cmdshell 'bcp [db_study].[dbo].[Inventory] out D: est.csv -S . -U sa -P sa -t "," -w '
三,將數(shù)據(jù)導(dǎo)入到SQL Server
CREATE TABLE [dbo].[Inventory_LoadIn]
(
[Store] [nvarchar](2) NULL,
[Item] [varchar](20) NULL,
[Color] [varchar](10) NULL,
[Quantity] [int] NULL
)
1,使用BCP,將txt文檔中的數(shù)據(jù)導(dǎo)入到table [dbo].[Inventory_LoadIn] 中
bcp [db_study].[dbo].[Inventory_LoadIn] in D: est.txt -S . -U sa -P sa -t " " -w bcp [db_study].[dbo].[Inventory_LoadIn] in D: est.csv -S . -U sa -P sa -t "," -w
2,使用xp_cmdshell 命令執(zhí)行bcp,將數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫table中
EXEC master..xp_cmdshell 'bcp [db_study].[dbo].[Inventory_LoadIn] in D: est.txt -S . -U sa -P sa -t " " -w ' EXEC master..xp_cmdshell 'bcp [db_study].[dbo].[Inventory_LoadIn] in D: est.csv -S . -U sa -P sa -t "," -w '
參考文檔:
bcp Utility
使用BCP導(dǎo)出導(dǎo)入數(shù)據(jù)
總結(jié)
以上是生活随笔為你收集整理的BCP 数据的导入和导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: customizing download
- 下一篇: research how javascr