SQL Server Bulk Insert 只需要部分字段時(shí)的方法
發(fā)布日期:2022-01-23 17:24 | 文章來(lái)源:源碼之家
我想到兩點(diǎn),1.手工修改格式化XML文件,2.創(chuàng)造一個(gè)能生成格式化XML文件的中間對(duì)象。
在MSDN中尋找方法時(shí),突然想到可以使用視圖來(lái)做中間對(duì)象,于是就搭一個(gè)測(cè)試下。以下是測(cè)試記錄:
USE master
GO
CREATE DATABASE [db_test]
GO
USE db_test
GO
CREATE TABLE dbo.T_test(
ID [int] IDENTITY(1,1) NOT NULL,
Code varchar(10) ,
Name varchar(100) ,
Memo nvarchar(500) ,
Memo2 ntext ,
PRIMARY KEY (ID)
)
GO
--上面創(chuàng)建的表是源數(shù)據(jù)表,下面創(chuàng)建是要導(dǎo)入數(shù)據(jù)的表,只有源表的三個(gè)字段
Select Code, Name,Memo into dbo.T_test2 from dbo.T_test Where 1=2 --需求就是把表T_test中的Code,Name導(dǎo)入到T_test2。
--接下來(lái),就是生成導(dǎo)入目標(biāo)表的格式化XML文件,可是MSDN上說(shuō)只能生成某個(gè)對(duì)象的格式化XML文件。
--只好建立一個(gè)中間對(duì)象來(lái)達(dá)到目的,這里我創(chuàng)建的是一個(gè)視圖。
--視圖只包含需要導(dǎo)入的字段
Create View v_test
AS
Select Code,Name From dbo.T_test2
--然后就是BCP操作
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE;
GO
EXEC master..xp_cmdshell 'BCP db_test.dbo.v_test format nul -f C:/v_test_fmt.xml -x -c -T -S MyPC\MyDB'
GO
EXEC master..xp_cmdshell 'BCP "select Code, Name from db_test.dbo.T_test" queryout C:/t_test.data -f C:/v_test_fmt.xml -T -S MyPC\MyDB'
GO --格式化文件和數(shù)據(jù)文件都有了,就成了.
BULK INSERT db_mgr.dbo.v_t1
FROM N'C:/t_test.data'
WITH
(
FORMATFILE = N'C:/v_test_fmt.xml'
)
GO
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 0;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE;
GO
Drop Database db_test
GO
環(huán)境是sql2005。
復(fù)制代碼 代碼如下:
USE master
GO
CREATE DATABASE [db_test]
GO
USE db_test
GO
CREATE TABLE dbo.T_test(
ID [int] IDENTITY(1,1) NOT NULL,
Code varchar(10) ,
Name varchar(100) ,
Memo nvarchar(500) ,
Memo2 ntext ,
PRIMARY KEY (ID)
)
GO
--上面創(chuàng)建的表是源數(shù)據(jù)表,下面創(chuàng)建是要導(dǎo)入數(shù)據(jù)的表,只有源表的三個(gè)字段
Select Code, Name,Memo into dbo.T_test2 from dbo.T_test Where 1=2 --需求就是把表T_test中的Code,Name導(dǎo)入到T_test2。
--接下來(lái),就是生成導(dǎo)入目標(biāo)表的格式化XML文件,可是MSDN上說(shuō)只能生成某個(gè)對(duì)象的格式化XML文件。
--只好建立一個(gè)中間對(duì)象來(lái)達(dá)到目的,這里我創(chuàng)建的是一個(gè)視圖。
--視圖只包含需要導(dǎo)入的字段
Create View v_test
AS
Select Code,Name From dbo.T_test2
--然后就是BCP操作
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE;
GO
EXEC master..xp_cmdshell 'BCP db_test.dbo.v_test format nul -f C:/v_test_fmt.xml -x -c -T -S MyPC\MyDB'
GO
EXEC master..xp_cmdshell 'BCP "select Code, Name from db_test.dbo.T_test" queryout C:/t_test.data -f C:/v_test_fmt.xml -T -S MyPC\MyDB'
GO --格式化文件和數(shù)據(jù)文件都有了,就成了.
BULK INSERT db_mgr.dbo.v_t1
FROM N'C:/t_test.data'
WITH
(
FORMATFILE = N'C:/v_test_fmt.xml'
)
GO
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 0;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE;
GO
Drop Database db_test
GO
環(huán)境是sql2005。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章