摘要:

   下文講述updatetext的功能及舉例説明

  實驗環境:sql server 2008 R2


updatetext關鍵字功能及語法説明

updatetext功能説明: updatetext的功能為:更新當前的text,ntext,image字段, 當我們可以使用updatetext更新列中的部分數據 updatetext語法簡介:

UPDATETEXT [BULK] { table_name.dest_column_name dest_text_ptr }  
       { NULL | insert_offset }  
       { NULL | delete_length }  
          [ WITH LOG ]  
          [ inserted_data  
           | { table_name.src_column_name src_text_ptr } ]

-----參數説明-------------------------

BULK:

是否採用二進制數據流,非採用二進制數據流,此參數無需輸入

table_name.dest_column_name table_name:

待更新的表名

dest_column_name:

待更新的列名(列需為text,ntext,image)類型,名稱必須符合相應的規則。

dest_text_ptr:

待更新text,ntext,image的值(需為二進制(16)),此值由textptr函數生成並返回。

insert_offset:

以0作為起始值, 在text,image列中,insert_offset為數據插入的開始值,(注意:ntext類型中每個字符佔用2個字節), 如果列中的值為null,則表示數據追加

delete_length:

由 insert_offset 位置開始的、要從現有 text、ntext 或 image 列中刪除的數據長度。 delete_length 值為 text 和 image 列指定時以字節為單位,為 ntext 列指定時以字符為單位。 每個 ntext 字符佔用 2 個字節。 值為 0 表示不刪除數據。 值為 NULL 則刪除現有 text 或 image 列中從 insert_offset 位置開始到末尾的所有數據。

WITH LOG:

在 SQL Server 2000 中被忽略。在 SQL Server 2005 中,日誌記錄由數據庫的實際恢復模型決定。

inserted_data

待插入到 insert_offset 位置現有 text、ntext 或 image 列中的數據。 此處數據可以由單個 char、nchar、varchar、nvarchar、binary、varbinary、text、ntext 或 image 值。 inserted_data 可以是文字或變量。

table_name.src_column_name:

用作插入數據源的表和 text、ntext 或 image 列的名稱。 表名和列名必須符合標識符規則。

src_text_ptr

指向用作插入數據源的 text、ntext 或 image 列的文本指針值(由 TEXTPTR 函數返回)。


updatetext關鍵字舉例應用

create table [maomao365.com]
(keyId int identity,
info ntext)
go

insert into [maomao365.com]
(info)values(N'sql博客教程'),
(N'sqlserver學習'),
(N'sqkserver愛好者')
go


---定義一個十六進制變量
declare @info_e binary(16)

--從源表中獲取變量信息
select @info_e = textptr(info)
 from [maomao365.com]
  where keyId =1 
  
---更新變量信息,
---從第二個字符開始,刪除字符為0,插入字段
updatetext [maomao365.com].info @info_e 
            2 0  N'新增字段'
 
---查看錶數據
select * from [maomao365.com]



go
 truncate table [maomao365.com]
 drop     table [maomao365.com]