大对象(Large Ojbect,简称LOB)是指那些包含任何数字化信息的数据字段,数字化信息可以是音频、视频、图像以及文档等。这类数据多以大容量文件的形式出现,如声音文件或图像文件等。

大对象数据可分为三种数据类型,即BLOB、CLOB和DBCLOB,分表表示二进制大对象数据(BINARY Large Object)、字符巨型对象数据(Character Large Object)和双字节巨型大对象(Double-Byte Character Large Object)数据。

 

BULK INSERT 语句

以用户指定的格式将数据文件导入数据库表或视图。

 Custemers.txt 文件

ABC/Jone/Sales Agent/Zhaong Row No.123/
Beijing//100082/china/654/654
def/Jone/Sales Agent/Zhong Road No.123/
Beijing/100082/china/654/654
ghi/jone/Sales Agent/ZhongGuan Road no.123/
jkl/Jone/Sales Agent/Zhong Road No.123

 

Bulk insert Customers from 'C:/Users/StarGate/Desktop/Custemers.txt'
 With (Fieldterminator = '/')--数据字段间以'/'为分隔符 

 

以下示例使用竖线 (|) 作为字段终止符,使用 |/n 作为行终止符,将订单详细信息从指定的数据文件导入 AdventureWorks.Sales.SalesOrderDetail 表中。
BULK INSERT AdventureWorks.Sales.SalesOrderDetail
   FROM 'f:/orders/lineitem.tbl'
   WITH 
      (
         FIELDTERMINATOR =' |',
         ROWTERMINATOR =' |/n'
      )

C. 使用换行符作为行终止符

以下示例将导入使用换行符作为行终止符的文件(如 UNIX 输出):

  
DECLARE @bulk_cmd varchar(1000)
SET @bulk_cmd = 'BULK INSERT AdventureWorks.Sales.SalesOrderDetail
FROM ''<drive>:/<path>/<filename>'' 
WITH (ROWTERMINATOR = '''+CHAR(10)+''')'
EXEC(@bulk_cmd)
----------------------------------------------------------------
file_format.txt
9.0
1
1 SQLIMage 0 19290 "" 4 Picture ""

 

declare @cateogryID int
Bulk insert Categories from 'C:/Users/Public/Pictures/Sample Pictures/Dock.jpg'
with(formatfile = 'C:/Users/StarGate/Desktop/file_format.txt')
set @cateogryID = Ident_Current('Categories')
update Categories set CategoryName = 'corn', Description = 'rice,petite,bean'
where categoryID = @cateogryID
select categoryID,categoryName,Description, datalength(Picture)
from Categories
where CategoryID = @cateogryID

 

--Ident_Current 胜于返回当前可用的最大自增弄字段,

 

Datalength()用于获取指定字段中的值

下面的示例返回在 Northwind 数据库的 Customers 表中列出的各个公司名的 DATALENGTH。
SELECT DATALENGTH(CompanyName), CompanyName
FROM Customers

 

注意 Bulk insert 使用时,对CategoryName 设置为'允许为空',否则在执行Bulk insert命令时会出现错误,因为该字段不空

 

 

有没有办法在不改变Cateogories 表结构的基础上完成大容量数据的输入工作呢?可以,但不使用bulk insert,而使用openrowset命令

--插入了一条新的记录
insert into Categories(CategoryName,[Description],Picture)
 select 'candy' as 类别名称,'chocalate,pana' as FileType,
 * from openrowset(bulk N'C:/Users/Public/Pictures/Sample Pictures/Dock.jpg',single_blob) as 图像

 

select categoryID ,categoryName,datalength(Picture) as 图像大小 from categories
where CategoryID = '9'

 

 

OlE自动化对象

 

--Microsoft Server 2005|[配置工具]|[Sql 外围应用配置器] 单击[功能的外围应用配置器]链接,选择OLE 自动化 启动

declare @Object int
declare @Hresult int
declare @Username varchar(255)
declare @ErrorSource varchar(255)
declare @ErrorDesc varchar(255)
declare @docHandle int
declare @doc int
declare @lCategoryID int, @lCategoryName varchar(15),@lDescription varchar(500)
declare @RecCount int
declare @tmpRecord varchar(5000)
declare @Record varchar(5000)
declare @appendText varchar(5000)
declare lCategories Cursor static
for
 select categoryID,categoryName,description from categories order by categoryid

 open lCategories

 select @RecCount = @@Cursor_rows
 set @Record= ''
 while @Record > 0
 begin
  fetch from lCategories
   into @lCategoryID,@lCategoryName,@lDescription
   set @tmpRecord = CONVERT(varchar(15),@lCategoryID) + '   '
    +@lCategoryName+'   '+@lDescription+Char(13)
   set @Record = Convert(varchar(5000),@tmpRecord) + Convert(varchar(5000),@Record)
   set @RecCount = @RecCount -1
 end

 close lCategories
 deallocate lCategories

 --Create the object
 exec @Hresult = sp_OACreate 'Word.Application',@Object out
 --Call the object's property and return the value
 exec @Hresult = sp_OAGetProperty @Object, 'UserName',@UserName out
 exec @Hresult = sp_OASetProperty @Object, 'Visible','True'

 --Destroy the object
 exec @Hresult = sp_OAGetProperty @Object,'Documents',@docHandle out
 exec @Hresult = sp_OAMethod @docHandle,'Add',@doc out

 set @appendText = 'selection.TypeText("'+Convert(varchar(5000),@Record)+'")'
 exec @Hresult = sp_OAMethod @Object,@appendText
 exec @Hresult = sp_OAMethod @Object,'ActiveDocument.SaveAS("C:/Users/StarGate/Documents/Categories.doc")'
 exec @Hresult = sp_OADestroy @Object

 if @Hresult <> 0
 begin
  exec sp_OAGetErrorInfo @Object,@ErrorSource out,@ErrorDesc out
  print '执行OLE对象时出现错误: ' + @ErrorSource + ' ' + @ErrorDesc
  return
 end
-- run 输入regedit 进入注册表编辑器 HKEY_CLASSES_ROOT/CLSID{000209FF-0000-0000-0000-000000000046}/ProgID}


本文转载:CSDN博客