




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
sql 中 更新text类型的字段-text字段增加处理-创建测试表create table test(id varchar(3),detail text)insert into testselect 001,A*B-定义添加的的字符串declare s_str varchar(8000),postion intselect s_str=*C -要添加的字符串 ,postion=null -追加的位置,null 加在尾部,0 加在首部,其他值则加在指定位置-字符串添加处理declare p varbinary(16)select p=textptr(detail) from test where id=001updatetext test.detail p postion 0 s_str-显示处理结果select * from testgo-删除测试表drop table test-text字段的替换处理-创建数据测试环境create table test(id varchar(3),txt text)insert into testselect 001,A*Bgo-定义替换的字符串declare s_str varchar(8000),d_str varchar(8000)select s_str=* -要替换的字符串 ,d_str=+ -替换成的字符串-字符串替换处理declare p varbinary(16),postion int,rplen intselect p=textptr(txt) ,rplen=len(s_str) ,postion=charindex(s_str,txt)-1from test where id=001while postion0begin updatetext test.txt p postion rplen d_str select postion=charindex(s_str,txt)-1 from testend-显示结果select * from testgo-删除数据测试环境drop table test-text字段的添加处理存储过程-全表-创建测试表create table user(uid int,UserLog text)create table order(uid int,state bit)insert into userselect 1,aunion all select 2,bunion all select 3,cinsert into orderselect 1,1union all select 2,0union all select 3,1go-处理的存储过程CREATE PROCEDURE spUpdateUserLogStrLog text,State intAS-定义游标,循环处理数据declare uid intdeclare #tb cursor for select a.uid from user a join order b on a.uid=b.uidwhere state=stateopen #tbfetch next from #tb into uidwhile fetch_status=0begin -字符串添加处理 declare p varbinary(16) select p=textptr(UserLog) from user where uid=uid updatetext user.UserLog p null 0 StrLog fetch next from #tb into uidendclose #tbdeallocate #tbgo-调用示例:exec spUpdateUserLog 123,1-显示处理结果select * from usergo-删除测试环境drop table user,orderdrop proc spUpdateUserLog/*-测试结果uid UserLog - -1 a1232 b3 c123(所影响的行数为 3 行)-*/-text字段的替换处理-全表替换-创建数据测试环境create table test(id varchar(3),txt text)insert into testselect 001,A*Bunion all select 002,A*B-AA*BBgo-定义替换的字符串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 testopen #tbfetch next from #tb into idwhile fetch_status=0begin -字符串替换处理 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=id while postion0 begin updatetext test.txt p postion rplen d_str select postion=charindex(s_str,txt)-1 from test where id=id end fetch next from #tb into idendclose #tbdeallocate #tb-显示结果select * from testgo-删除数据测试环境drop table test*支持text字段处理的仅有:下面的函数和语句可以与 ntext、text 或 image 数据一起使用。函数 语句DATALENGTH READTEXTPATINDEX SET TEXTSIZESUBSTRING UPDATETEXTTEXTPTR WRITETEXTTEXTVALID1:替换-创建数据测试环境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 intselect p=textptr(aa),rplen=len(s_str),postion=charindex(s_str,aa)-1 from #tbwhile postion0beginupdatetext #tb.aa p postion rplen d_strselect postion=charindex(s_str,aa)-1 from #tbend-显示结果select * from #tb-删除数据测试环境drop table #tb/*全部替换*/DECLARE ptrval binary(16)SELECT ptrval = TEXTPTR(aa)FROM#tbWHERE 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 intselect p=textptr(deta
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年性病科性病常识及健康教育考核试题及答案解析
- 产业政策影响分析-第2篇-洞察及研究
- 2025年北京市农业科技合作项目合同范本
- 2025年用工单位是否强制要求签订劳动合同
- 2025临时工劳务合同新范文
- 2025企业运营指南合同管理范本
- 2025安全评估服务合同
- 2025车辆抵押合同
- 更期保健与荷尔蒙疗法2讲课文档
- 2025劳动合同协议方案协同保证契约书
- 2025年房地产项目合作开发与城市更新协议
- 接种疫苗预防流感课件
- 《中国金融学》课件 第0章 绪论-课件
- 2025年中国烟草总公司天津市公司招聘考试笔试试卷【附答案】
- 加盟合同(标准版)
- 基于plc的恒压供水控制系统设计
- 环保设备加工处理方案(3篇)
- 2025中小学生法制知识竞赛题库及答案
- 【高三】【数学】2025【秋】开学第一课:为梦想飞翔(课件)
- 员工安全手册
- 屋面防水施工合同的范本
评论
0/150
提交评论