如何调用 SQL 服务器中变量的方法

How to call methods on a variable in SQL Server

我在培训视频中看到了以下语法,我想知道这叫什么以及我们可以对变量使用哪些方法?

 DECLARE @text VARCHAR(max);
 SET @text = REPLICATE(CAST('asdfasdf' as varchar(max)),8000);
 SET @text.write('zzzzzzzz',0,8);
 SELECT @text

变量中存储的值为 'zzzzzzzzasdfasdf...'

这不是一种方法,尽管它确实看起来像。与您可以在 SELECT 中使用的 XML methods 不同,这是一个修改器。它在指定了 max 长度的 (n)varchar 和 varbinary 数据类型上可用,用于有效地修改存储的值。

关于他们的信息不多,我只找到了this blog post

update.write 语法并不是真正的方法。它是一种用于更新 nvarchar(max) 等大型数据类型的 TSQL 语法。

官方UPDATE documentation介绍了.WRITE的使用方法

  SET  
        { column_name = { expression | DEFAULT | NULL }  
          | { udt_column_name.{ { property_name = expression  
                                | field_name = expression }  
                                | method_name ( argument [ ,...n ] )  
                              }  
          }  
          | column_name { .WRITE ( expression , @Offset , @Length ) } 

.WRITE (expression,@Offset,@Length) Specifies that a section of the value of column_name is to be modified. expression replaces @Length units starting from @Offset of column_name. Only columns of varchar(max), nvarchar(max), or varbinary(max) can be specified with this clause. column_name cannot be NULL and cannot be qualified with a table name or table alias.

expression is the value that is copied to column_name. expression must evaluate to or be able to be implicitly cast to the column_name type. If expression is set to NULL, @Length is ignored, and the value in column_name is truncated at the specified @Offset.

@Offset is the starting point in the value stored in column_name at which expression is written. @Offset is a zero-based ordinal byte position, is bigint, and cannot be a negative number. If @Offset is NULL, the update operation appends expression at the end of the existing column_name value and @Length is ignored. If @Offset is greater than the byte length of the column_name value, the Database Engine returns an error. If @Offset plus @Length exceeds the end of the underlying value in the column, the deletion occurs up to the last character of the value.

@Length is the length of the section in the column, starting from @Offset, that is replaced by expression. @Length is bigint and cannot be a negative number. If @Length is NULL, the update operation removes all data from @Offset to the end of the column_name value.

另见 Updating Large Data Types and the examples provided for updating large data types