SqlServer 对 数据类型 text 的操作
--text字段增加处理--创建测试表 create table test(id varchar(3),detail text) insert into test select '001','A*B'--定义添加的的字符串 declare @s_str varchar(8000),@postion int select @s_str='*C' --要添加的字符串,@postion=null --追加的位置,null 加在尾部,0 加在首部,其他值则加在指定位置--字符串添加处理 declare @p varbinary(16) select @p=textptr(detail) from test where id='001' updatetext test.detail @p @postion 0 @s_str--显示处理结果 select * from test go--删除测试表 drop table test--text字段的替换处理--创建数据测试环境 create table test(id varchar(3),txt text) insert into test select '001','A*B' go--定义替换的字符串 declare @s_str varchar(8000),@d_str varchar(8000) select @s_str='*' --要替换的字符串,@d_str='+' --替换成的字符串--字符串替换处理 declare @p varbinary(16),@postion int,@rplen int select @p=textptr(txt),@rplen=len(@s_str),@postion=charindex(@s_str,txt)-1 from test where id='001'while @postion>0 beginupdatetext test.txt @p @postion @rplen @d_strselect @postion=charindex(@s_str,txt)-1 from test end--显示结果 select * from testgo --删除数据测试环境 drop table test--text字段的添加处理存储过程--全表--创建测试表 create table [user](uid int,UserLog text) create table [order](uid int,state bit)insert into [user] select 1,'a' union all select 2,'b' union all select 3,'c'insert into [order] select 1,1 union all select 2,0 union all select 3,1 go--处理的存储过程 CREATE PROCEDURE spUpdateUserLog @StrLog text, @State int AS --定义游标,循环处理数据 declare @uid int declare #tb cursor for select a.uid from [user] a join [order] b on a.uid=b.uid where state=@stateopen #tb fetch next from #tb into @uid while @@fetch_status=0 begin--字符串添加处理declare @p varbinary(16)select @p=textptr(UserLog) from [user] where uid=@uidupdatetext [user].UserLog @p null 0 @StrLogfetch next from #tb into @uid end close #tb deallocate #tb go--调用示例: exec spUpdateUserLog '123',1--显示处理结果 select * from [user]go--删除测试环境 drop table [user],[order] drop proc spUpdateUserLog--text字段的替换处理--全表替换--创建数据测试环境 create table test(id varchar(3),txt text) insert into test select '001','A*B' union all select '002','A*B-AA*BB' go--定义替换的字符串 declare @s_str varchar(8000),@d_str varchar(8000) select @s_str='*' --要替换的字符串,@d_str='+' --替换成的字符串--定义游标,循环处理数据 declare @id varchar(3) declare #tb cursor for select id from test open #tb fetch next from #tb into @id while @@fetch_status=0 begin--字符串替换处理declare @p varbinary(16),@postion int,@rplen intselect @p=textptr(txt),@rplen=len(@s_str),@postion=charindex(@s_str,txt)-1from test where id=@idwhile @postion>0beginupdatetext test.txt @p @postion @rplen @d_strselect @postion=charindex(@s_str,txt)-1 from test where id=@idendfetch next from #tb into @id end close #tb deallocate #tb--显示结果 select * from testgo --删除数据测试环境 drop table test/*支持text字段处理的仅有: 下面的函数和语句可以与 ntext、text 或 image 数据一起使用。 函数 语句 DATALENGTH READTEXT PATINDEX SET TEXTSIZE SUBSTRING UPDATETEXT TEXTPTR WRITETEXT TEXTVALID */--1:替换--创建数据测试环境 create table #tb(aa text) insert into #tb select 'abc123abc123,asd'--定义替换的字符串 declare @s_str varchar(8000),@d_str varchar(8000) select @s_str='123' --要替换的字符串 ,@d_str='000' --替换成的字符串--字符串替换处理 declare @p varbinary(16),@postion int,@rplen int select @p=textptr(aa),@rplen=len(@s_str),@postion=charindex(@s_str,aa)-1 from #tb while @postion>0 begin updatetext #tb.aa @p @postion @rplen @d_str select @postion=charindex(@s_str,aa)-1 from #tb end--显示结果 select * from #tb--删除数据测试环境 drop table #tb/****************全部替换************************/ DECLARE @ptrval binary(16) SELECT @ptrval = TEXTPTR(aa) FROM #tb WHERE aa like '%数据2%' if @ptrval is not null -- 一定要加上此句,否则若找不到数据下一句就会报错 UPDATETEXT #tb.aa @ptrval 0 null '数据3'/****************在字段尾添加**********************************/ --定义添加的的字符串 declare @s_str varchar(8000) select @s_str='*C' --要添加的字符串 --字符串添加处理 declare @p varbinary(16),@postion int,@rplen int select @p=textptr(detail) from test where id='001' updatetext test.detail @p null null @s_str/*总结: 1:Text字段类型不能直接用replace函数来替换,必须用updatetext 2:字段比较不能用 where 字段 = ‘某数据’,可以用like来代替 3:updatetext时,若@ptrval值为空会出错,需注意*/
转载于:https://www.cnblogs.com/refactor/archive/2012/07/09/2575995.html
SqlServer 对 数据类型 text 的操作相关推荐
- SqlServer常用数据类型
SqlServer常用数据类型 一.ASCII字符型 String 类型: Ascll字符型:数据类型包括char.varchar.text 数据类型包括char.varchar.textAscl ...
- [Microsoft][ODBC SQL Server Driver][SQl Server]参数数据类型 text 对于 replace 函数的参数 1 无效。...
说到数据库中文本的替换,一般人首先想到的是Replace函数: update Table set Column=Replace(Column,'oldkeyword','newkeyword') 不过 ...
- Tensorflow |(2)张量的阶和数据类型及张量操作
Tensorflow |(1)初识Tensorflow Tensorflow |(2)张量的阶和数据类型及张量操作 张量的阶和数据类型 TensorFlow用张量这种数据结构来表示所有的数据.你可以把 ...
- nodejs操作sqlserver数据_nodejs基于mssql模块连接sqlserver数据库的简单封装操作示例...
本文实例讲述了nodejs基于mssql模块连接sqlserver数据库的简单封装操作.分享给大家供大家参考,具体如下: 注意:开启sqlserver服务器允许远程连接的步骤,自行百度,很多经验,no ...
- vue-html5-editor接收数据,在vue中获取wangeditor的html和text的操作
目的:vue 中获取 wangeditor 的 html 和 text. 补充知识:vue-cli webpack 引入 wangeditor(轻量级富文本框) 1:使用npm下载: //(注意 wa ...
- “数据源“SqlServer ”包含无法用于处理操作的 ImpersonationMode。不能与名称为“SqlServer ”的数据源进行连接。”
SSMS导入SSAS表格项目报错 "数据源"SqlServer "包含无法用于处理操作的 ImpersonationMode.不能与名称为"SqlServer ...
- redis五种数据类型及其常见操作
redis五种数据类型及其常见操作 Redis支持5种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 1.strin ...
- C++通过ODBC方式连接数据库SQLServer及增删查改操作【图书借阅系统为例】
C++通过ODBC方式连接数据库SQLServer及增删查改操作[图书借阅系统为例] 文章目录 前言 一.ODBC如何配置 二.SQL Server如何设置账号密码 三.C++连接数据库以及增删查改操 ...
- sql sever 数据类型 text 和 varchar 在 equal to 运算符中不兼容。
背景: 两个不同项目中的同样的表,将A表中一列里某个单元格(text数据类型)的内容粘到B表对应位置(text数据类型),报错"数据类型 text 和 varchar 在 equal to ...
最新文章
- 使用google云(GCP)二次利用安装kali Linux(kali browser)
- windows下钩子的使用
- 拓扑学+计算机,吴国平: 拓扑学到底有多重要? 在数学中占据多高的地位?
- Redis的设计与实现之跳表
- SAP Spartacus里的converter实例化逻辑
- jq获取页面高度_JQ获取窗口文档等等高度总结!
- 通过规模化Scrum创造最新技术的打印机
- 编程经历的一些思考——一个工作4年老鸟的职业思考(转载)
- Linux 文件,目录的属性
- linux 脚本 过滤 词,linux学习之shell脚本 - 文本过滤
- python续行_python的续行
- 微信小程序——打开地图 选择位置 完整功能实现代码(定位,检索周边,可移动选点,可搜索,腾讯地图API)
- Windows桌面端录屏采集实现
- Linux下使用uinput创建虚拟设备(Ubuntu20.04.2)
- 两大热门技术碰撞 论区块链在物联网中的应用
- DirectPlay的基本概念
- java lamda 常用写法 List篇
- 专业相关的计算机知识领域,计算机技术(领域)
- uniapp页面显示服务器图片,uniapp实现加载图片失败显示占位图
- 【大葱虽有4大治病功效】
热门文章
- Ruoyi框架Maven编译不通过的问题---SpringCloud Alibaba_若依微服务框架改造---工作笔记006
- 大数据_Hbase-(概念补充_hbase中namespace的概念)---Hbase工作笔记0007
- STM32工作笔记0017---ISP串口下载
- System学习笔记007---win10连接阿里云出现_远程连接提示要求的函数不受支持如何解决
- SpringCloud学习笔记022---SpringBoot中集成使用MongoDb进行增删改查
- 显示脸上的关键点的程序
- mybatis直接执行sql语句后续之一
- SVM支持向量机,我用到的自学材料
- 杭电2151 Worm dp
- c++ xml 解析“后直接跟值问题