替换一个数据库中的关键字

declare @SqlStr varchar(8000)
declare @SQLStrForU varchar(8000)

declare @TableName varchar(50)
declare @TableID bigint

declare @ColumnName varchar(50)


declare auth_cur cursor for
select Name,object_id from sys.all_objects where Type='U'
open auth_cur
fetch next from auth_cur into @TableName,@TableID
while (@@fetch_status=0)
begin
set @SqlStr='select '
set @SQLStrForU='update '+@TableName+' set '
declare auth_cur1 cursor for
select a.Name from sys.all_columns a join sys.types b
on a.system_type_id=b.system_type_id
where a.object_id=@TableID and b.Name in('varchar','char','nvarchar')
open auth_cur1
fetch next from auth_cur1 into @ColumnName
while (@@fetch_status=0)
begin
set @SqlStr=@SqlStr+@ColumnName+','
set @SQLStrForU=@SQLStrForU+@ColumnName+'=replace('+@ColumnName+',''成都市'',''北京市''),'
fetch next from auth_cur1 into @ColumnName
end
close auth_cur1
deallocate auth_cur1
set @SqlStr=substring(@SqlStr,1,len(@SqlStr)-1)
set @SQLStrForU=substring(@SQLStrForU,1,len(@SQLStrForU)-1)

set @SqlStr=@SqlStr+' from '+@TableName
print @SQLStrForU

exec(@SQLStrForU)

fetch next from auth_cur into @TableName,@TableID
end
close auth_cur
deallocate auth_cur

posted @ 2016-03-24 13:30  三瑞  阅读(504)  评论(0编辑  收藏  举报