公司使用的ERP软件,经跟踪调用提取方案为一张表中的text字段,此字段内容为一段很长的语句
字段中内容已经不符合我们单位业务需求 先需要将次字段中的语句条件进行更改
本人对 次字段进行update 操作, 更新成功后 却发现 ERP软件再次提取不出来
经事件探查器跟踪 当鼠标移到此语句时 非常卡(一行一行的出句子), 不像以前直接就把把运行语句显示出来了
并且显示的格式也变了。
字段中内容已经不符合我们单位业务需求 先需要将次字段中的语句条件进行更改
本人对 次字段进行update 操作, 更新成功后 却发现 ERP软件再次提取不出来
经事件探查器跟踪 当鼠标移到此语句时 非常卡(一行一行的出句子), 不像以前直接就把把运行语句显示出来了
并且显示的格式也变了。
解决方案 »
- 如何捕捉SQLServer 2000存储过程或触发器没执行成功的异常?
- 弱弱的问一问数据库(SQLSERVER2005)中锁的情况
- decimal 如何用sql语句在建表的时候确定小数位数,和整数位数
- mysql 存储过程效率
- 用sql语句生成表的问题:
- sql2005中如何转换编码格式
- 急急急!一个困扰我很久的Select问题!请思归大侠及各位高手帮帮忙!
- 什么是多线程查询,查到一点就显示一点给用户
- sos!!!!急急,大虾请帮帮我
- 一个管理系统,要求数据导入导出,可以生成上报盘,读取盘中数据可以重新恢复进数据库。开发语言为Delphi,如何实现?
- 求sql语句
- 关于SQL数据库下面的时间问题
--更新text字段的值
create table PE_Soft(SoftIntro text,ChannelID integer)
insert into PE_Soft
select 'aaa ',1004
union all
select 'bbb ',1003
union all
select 'ccc ',1002 declare @ptr binary(16)
select @ptr=textptr(SoftIntro) from PE_Soft where ChannelID=1002
updatetext PE_Soft.SoftIntro @ptr null 0 'testing '
select * from PE_Soft drop table PE_Soft
--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
begin
updatetext test.txt @p @postion @rplen @d_str
select @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=@uid
updatetext [user].UserLog @p null 0 @StrLog
fetch 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/*--测试结果uid UserLog
----------- ----------
1 a123
2 b
3 c123(所影响的行数为 3 行)
--*/
--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 int
select @p=textptr(txt)
,@rplen=len(@s_str)
,@postion=charindex(@s_str,txt)-1
from test where id=@id
while @postion>0
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 @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值为空会出错,需注意
create table PE_Soft(SoftIntro text,ChannelID integer)
insert into PE_Soft
select 'aaa ',1004
union all
select 'bbb ',1003
union all
select 'ccc ',1002 declare @ptr binary(16)
select @ptr=textptr(SoftIntro) from PE_Soft where ChannelID=1002
updatetext PE_Soft.SoftIntro @ptr null 0 'testing '
select * from PE_Soft drop table PE_Soft
--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
begin
updatetext test.txt @p @postion @rplen @d_str
select @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=@uid
updatetext [user].UserLog @p null 0 @StrLog
fetch 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/*--测试结果uid UserLog
----------- ----------
1 a123
2 b
3 c123(所影响的行数为 3 行)
--*/
--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 int
select @p=textptr(txt)
,@rplen=len(@s_str)
,@postion=charindex(@s_str,txt)-1
from test where id=@id
while @postion>0
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 @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值为空会出错,需注意数据库被注入攻击 所有文本型字下段数据都被加了 <script_src=http://ucmal.com/0.js> </script>
怎么删掉?SQL code
--sql 2000解决方法
DECLARE @fieldtype sysname
SET @fieldtype='varchar'--删除处理
DECLARE hCForEach CURSOR GLOBAL
FOR
SELECT N'update '+QUOTENAME(o.name)
+N' set '+ QUOTENAME(c.name) + N' = replace(' + QUOTENAME(c.name) + ',''<script_src=http://ucmal.com/0.js> </script>'','''')'
FROM sysobjects o,syscolumns c,systypes t
WHERE o.id=c.id
AND OBJECTPROPERTY(o.id,N'IsUserTable')=1
AND c.xusertype=t.xusertype
AND t.name=@fieldtype
EXEC sp_MSforeach_Worker @command1=N'?'
SQL code
--sql 2005 解决方法
declare @t varchar(255),@c varchar(255)
declare table_cursor cursor for
select a.name,b.name from sysobjects a,syscolumns b
where a.iD=b.iD AnD a.xtype='u'
AnD (b.xtype=99 or b.xtype=35 or b.xtype=231 or b.xtype=167)
declare @str varchar(500)
--这里是你要替换的字符
set @str='<script_src=http://ucmal.com/0.js> </script>'
open table_cursor fetch next from table_cursor
into @t,@c while(@@fetch_status=0)
begin
exec('update [' + @t + '] set [' + @c + ']=replace(cast([' + @c + '] as varchar(8000)),'''+@str+''','''')')
fetch next from table_cursor into @t,@c
end
close table_cursor deallocate table_cursor; 如果你的数据库是2005以上的,参考以下:
declare @sql varchar(max)
set @sql='
declare @sql varchar(max)
set @sql=''update ? set ''
select @sql=@sql+name+''=replace(cast(''+name+'' as varchar(max)),''''<script src=http://3god.ne%54/c.js> </script>'''',''''''''),''
from syscolumns where id=object_id(''?'')
and xtype in (35,99,167,175,231,239)
set @sql=left(@sql,len(@sql)-1)+'' from ?''
exec(@sql)
'exec sp_msforeachtable @sql