BULK INSERT pubs..publishers2 FROM 'c:\newpubs.dat'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Newpubs.dat 文件中的数据现在已追加到 publishers2 中:
Pub_id pub_name city state Country
------ ---------------- ---------- ----- -----
0736 New Moon Books Boston MA USA
0877 Binnet & Hardley Washington DC USA
1111 Stone Age Books Boston MA USA
1389 Algodata Infosystems Berkeley CA USA
1622 Five Lakes Publishing Chicago IL USA
1756 Ramona Publishers Dallas TX USA
2222 Harley & Davidson Washington DC USA
3333 Infodata Algosystems Berkeley CA USA
9901 GGG&G München Germany
9952 Scootney Books New York NY USA
9999 Lucerne Publishing Paris France
复制包含标识值的数据
bcp 实用工具和 BULK INSERT 语句允许将包含标识值的数据文件大容量复制到 SQL Server 实例中。为防止 SQL Server 提供标识值,bcp 实用工具接受 -E 开关,并且 BULK INSERT 语句接受 KEEPIDENTITY 子句。当数据文件中的行大容量复制到表中时,SQL Server 不会自动地指派唯一标识值;标识值将从数据文件中获得。
如果未提供这些选项,则将忽略被导入数据文件中标识符列的值,而是由 SQL Server 根据表创建期间指定的种子值和增量值自动指派唯一值。如果数据文件不包含表中标识符列的值,则使用格式文件指定导入数据时应跳过的标识符列。SQL Server 自动赋给该列唯一值。
bcp pubs..bitmap in test.doc -Usa -Ppassword -Sservername
bcp 将提示:
Enter the file storage type of field c1 [image]:
Enter the prefix length of field c1 [4]: 0
Enter length of field c1 [4096]: 5578
Enter the field terminator [none]:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_binaryIO]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_binaryIO]
GO
--则取得导入文件的大小
if @isout=1
set @fsize='0'
else
begin
create table #tb(可选名 varchar(20),大小 int
,创建日期 varchar(10),创建时间 varchar(20)
,上次写操作日期 varchar(10),上次写操作时间 varchar(20)
,上次访问日期 varchar(10),上次访问时间 varchar(20),特性 int)
insert into #tb
exec master..xp_getfiledetails @fname
select @fsize=大小 from #tb
drop table #tb
if @fsize is null
begin
print '文件未找到'
return
end
end
--生成数据处理应答文件
set @m_tbname='[##temp'+cast(newid() as varchar(40))+']'
set @sql='select * into '+@m_tbname+' from(
select null as 类型
union all select 0 as 前缀
union all select '+@fsize+' as 长度
union all select null as 结束
union all select null as 格式
) a'
exec(@sql)
select @fname_in=@fname+'_temp'
,@sql='bcp "'+@m_tbname+'" out "'+@fname_in
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'" /c'
exec master..xp_cmdshell @sql
--删除临时表
set @sql='drop table '+@m_tbname
exec(@sql)
if @isout=1
begin
set @sql='bcp "select top 1 '+@fdname+' from '
+@tbname+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
+'" queryout "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
end
else
begin
--为数据导入准备临时表
set @sql='select top 0 '+@fdname+' into '
+@m_tbname+' from ' +@tbname
exec(@sql)
--将数据导入到临时表
set @sql='bcp "'+@m_tbname+'" in "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
--将数据导入到正式表中
set @sql='update '+@tbname
+' set '+@fdname+'=b.'+@fdname
+' from '+@tbname+' a,'
+@m_tbname+' b'
+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
exec(@sql)
--删除数据处理临时表
set @sql='drop table '+@m_tbname
end
--删除数据处理应答文件
set @sql='del '+@fname_in
exec master..xp_cmdshell @sql