​https://docs.microsoft.com/zh-cn/sql/t-sql/queries/writetext-transact-sql?view=sql-server-ver15​

SqlServer的T-Sql

如下:


表结构:

字段名         id      title      content

类型            int      char(200)   text

Insert Into News (title,content) Values (@title,@content)


实际上这样插入是不能超过8000字节的(content字段)。SqlServer在这方面做了限制。


可以这样插入



【转】SqlServer Text类型字段超过8000字处理_sqlCREATE PROCEDURE NewsInsert   @title char(200),@content text   AS

【转】SqlServer Text类型字段超过8000字处理_sql

【转】SqlServer Text类型字段超过8000字处理_sqlInsert Into News (title,content) Values (@title,'')

【转】SqlServer Text类型字段超过8000字处理_sql

【转】SqlServer Text类型字段超过8000字处理_sqlDECLARE @ptrval binary(16)

【转】SqlServer Text类型字段超过8000字处理_sqlSELECT @ptrval = TEXTPTR(content) 

【转】SqlServer Text类型字段超过8000字处理_sqlFROM News 

【转】SqlServer Text类型字段超过8000字处理_sqlWHERE id = @@identity

【转】SqlServer Text类型字段超过8000字处理_sqlwriteTEXT News .content @ptrval  @content

【转】SqlServer Text类型字段超过8000字处理_sql

【转】SqlServer Text类型字段超过8000字处理_sqlGO



用到了writeTEXT函数。

注意:插入的时候Insert Into News (title,content) Values (@title,'')一定要有content值对应空,不能让content是null状态.否则下面的无法找到地址。



更新的时候:


【转】SqlServer Text类型字段超过8000字处理_sqlCREATE PROCEDURE NewsInsert   @title char(200),@content text,@id int   AS

【转】SqlServer Text类型字段超过8000字处理_sql

【转】SqlServer Text类型字段超过8000字处理_sqlUpdate News Set title = @title,content='' Where id = @id --注意content=''虽然不起作用,但是最好写上,避免content有null的情况

【转】SqlServer Text类型字段超过8000字处理_sql

【转】SqlServer Text类型字段超过8000字处理_sqlDECLARE @ptrval binary(16)

【转】SqlServer Text类型字段超过8000字处理_sqlSELECT @ptrval = TEXTPTR(content) 

【转】SqlServer Text类型字段超过8000字处理_sqlFROM News 

【转】SqlServer Text类型字段超过8000字处理_sqlWHERE id = @id

【转】SqlServer Text类型字段超过8000字处理_sqlwriteTEXT News .content @ptrval  @content

【转】SqlServer Text类型字段超过8000字处理_sql

【转】SqlServer Text类型字段超过8000字处理_sqlGO


读取和删除的时候一切正常,就不多叙述了。

以上用法可以插入数据库类型Text对应的理论实际长度以内。