SQL語句 SQL Server中Text型別操作

2021-06-22 18:34:44 字數 2843 閱讀 3565

基本方法:

updatetext // [ with log ] [ inserted_data | ]

新插入的資料可以是單個inserted_data 常量、表名、列名或文字指標

update 

操作

updatetext 

引數

替換現有資料

指定乙個非空

insert_offset 

值、非零

delete_length 

值和要插入的新資料。

刪除現有資料

指定乙個非空

insert_offset 

值、非零

delete_length 

值和要插入的新資料。不指定要插入的新資料。

插入新資料 指定

insert_offset 

值、為0 

的delete_length 

值和要插入的新資料。

為獲得最佳效能,建議在插入或更新text、ntext 和image 資料時,使用8,040 位元組的倍數的塊區大小。

insert_offset

以零為基的更新起始位置。對於 text 或 image 列,

insert_offset 是在插入新資料前要從現有列的起點跳過的位元組數。對於 ntext 列,

insert_offset 是字元數(每個 ntext 字元占用 2 個位元組)。從此基數為零的起始點開始的現有

text、ntext 或 image 資料向右移,為新資料留出空間。值為 0 表示將新資料插入現有資料的開始處。

值為 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

要插入現有 text、ntext 或 image 列的 insert_offset 位置的資料。這是單個 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 函式返回)。

指向用作插入資料來源的

text

、ntext 

或image 

列的文字指標值(由

textptr 

函式返回)。

eg.

在字段operlog

(order_flight

)後追加

11

declare @ptrval binary(16)

select @ptrval=textptr(operlog) from order_flight where id=12475

updatetext order_flight.operlog @ptrval null 0 '11'

eg.

刪除表test

中欄位mytext

的前兩個字元

declare @ptrval binary(16)

select @ptrval=textptr(mytext) from test where id=2

updatetext test.mytext @ptrval  0 2 –(0

是刪除起始位置,

2-刪除字串長度

)eg.

替換test

中id=2

的字段mytext

中所有』333』

為』abc』

declare @s_str varchar(8000),@d_str varchar(8000)

select @s_str='333' --

要替換的字串

,@d_str='abc' --

替換成的字串

declare @p varbinary(16),@postion int,@rplen int

select @p=textptr(mytext),@rplen=len(@s_str),@postion=charindex(@s_str,mytext)-1 from test where id = 2

while @postion>=0

begin

updatetext test.mytext @p @postion @rplen @d_str

select @postion=charindex(@s_str,mytext)-1 from test where id = 2

end

sqlserver中動態sql語句應用

所有人知道select from tablename where aa bb的用法 大部分人知道 exec select from tablename where aa bb 的用法 但是仍然有很多人不知道sp executesql的用法,他可以讓動態sql接收引數且把查詢結果返回到乙個引數 接收條...

sqlserver中動態sql語句應用

所有人知道select from tablename where aa bb的用法 大部分人知道 exec select from tablename where aa bb 的用法 但是仍然有很多人不知道sp executesql的用法,他可以讓動態sql接收引數且把查詢結果返回到乙個引數 接收條...

sqlserver中動態sql語句應用

所有人知道select from tablename where aa bb的用法 大部分人知道 exec select from tablename where aa bb 的用法 但是仍然有很多人不知道sp executesql的用法,他可以讓動態sql接收引數且把查詢結果返回到乙個引數 接收條...