溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

發(fā)布時間:2020-08-05 13:31:44 來源:網(wǎng)絡(luò) 閱讀:443 作者:lzwxx 欄目:網(wǎng)絡(luò)安全

BCP 命令的參數(shù)很多,使用 -h 查看幫助信息,注意:參數(shù)是區(qū)分大小寫的

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

使用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類型做為存儲類型,沒有前綴且以"\t"做為字段分割符,以"\n"做為行分割符。

-w 使用Unicode字符集拷貝數(shù)據(jù),在數(shù)據(jù)庫中,需要將Table Column設(shè)置為 nchar或nvarchar存儲類型。如果 -c 和 -w 同時指定,那么 -w 將覆蓋 -c。

-t field_term 指定column分割符,默認是"\t"。

-r row_term 指定row分割符,默認是"\n"。

-S server_name[ \instance_name] 指定要連接的SQL Server服務(wù)器的實例,如果未指定此選項,BCP連接本機的SQL Server默認實例。如果要連接某臺機器上的默認實例,只需要指定機器名即可。

-U login_id 指定連接SQL Sever的用戶名。

-P password 指定連接SQL Server的用戶名密碼。

-T 指定BCP使用信任連接登錄SQL Server。如果未指定-T,必須指定-U和-P。

-d 指定數(shù)據(jù)庫名稱

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

 

一,使用bcp 將整個table中的數(shù)據(jù)導(dǎo)出到txt或csv文檔中

bcp db_study.dbo.sales out D:\test.txt S . U sa P sa t  S . U sa P sa t  w

二,使用 query statement 將查詢結(jié)果導(dǎo)出到txt 或 csv文檔中

1,配置SQL Server,允許運行 xp_cmdshell 命令

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

-- 允許配置高級選項  EXEC master.sys.sp_configure 'show advanced options', 1  -- 重新配置  RECONFIGURE  -- 啟用xp_cmdshell  EXEC master.sys.sp_configure 'xp_cmdshell', 1  --重新配置  RECONFIGURE

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

否則,SQL Server 會拋出錯誤信息

SQL Server 阻止了對組件“xp_cmdshell”的 過程“sys.xp_cmdshell”的訪問,因為此組件已作為此服務(wù)器安全配置的一部分而被關(guān)閉。系統(tǒng)管理員可以通過使用 sp_configure 啟用“xp_cmdshell”。有關(guān)啟用“xp_cmdshell”的詳細信息,請搜索 SQL Server 聯(lián)機叢書中的“xp_cmdshell”。

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

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

-- 允許配置高級選項  EXEC master.sys.sp_configure 'show advanced options', 1  -- 重新配置  RECONFIGURE  -- 禁用xp_cmdshell  EXEC master.sys.sp_configure 'xp_cmdshell', 0--重新配置  RECONFIGURE

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

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:\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的數(shù)據(jù)導(dǎo)出

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 '


三,將數(shù)據(jù)導(dǎo)入到SQL Server

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

CREATE TABLE [dbo].[Inventory_LoadIn](    [Store] [nvarchar](2) NULL,    [Item] [varchar](20) NULL,    [Color] [varchar](10) NULL,    [Quantity] [int] NULL)

BCP 數(shù)據(jù)的導(dǎo)入和導(dǎo)出

1,使用BCP,將txt文檔中的數(shù)據(jù)導(dǎo)入到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 命令執(zhí)行bcp,將數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫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 '


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

bcp
AI