参考csdn realgz的:两个支持text字段内文字替换的存储过程
csdn realgz
(http://topic.csdn.net/u/20080505/20/d2dffbbe-6d6b-41cf-b29a-41149540eafa.html?seed=562172452)if object_id('sp_replaceTextWithMultiColPk') is not null
drop proc sp_replaceTextWithMultiColPk
go create procedure sp_replaceTextWithMultiColPk
@tableName sysname,@colName sysname,@oldStr nvarchar(512),@newStr nvarchar(512),@whereStr nvarchar(200)=''
/*
为一个表内的text做统一替换的存储过程
缺点:慢,而且事实上还没办法支持任意表,而且代码看起来难受
因为 varchar的长度有限,当然还可以扩展,不过在sql 2005 里已经没这个问题了,懒得再写。
realgz 2008-05-05 */
as
begin
set nocount on
declare @cursor nvarchar(4000),@fetch nvarchar(4000),@insert nvarchar(4000),@where nvarchar(4000)
declare @replaceExec nvarchar(4000),@checkExec nvarchar(4000)
declare @rpPtr varbinary(16),@rpPostion int,@rpLen int
if object_id('tempdb..#key') is not null
drop table #key --获得主键列表
select sc.name keyName into #key from
sysobjects so
join sysindexes idx on so.parent_obj=idx.id
join sysindexkeys idk on so.parent_obj=idk.id and idx.indid = idk.indid
join syscolumns sc on so.parent_obj=sc.id and idk.colid=sc.colid
where so.xtype='PK' and so.parent_obj =object_id(@tableName) and idx.status & 0x800>0
if @@rowcount < 1
begin
raiserror ('表不符合要求或者没有这个表',12,1)
return
end
--增加键值列
alter table #key add keyValue sql_variant
alter table #key add primary key(keyName) --替换的长度
select @rpLen=len(@oldStr),@oldStr='%'+@oldStr+'%' --游标声明语句
select @cursor ='declare #c cursor static for select textptr('+@colname+'),PATINDEX ('''+replace(@oldStr,'''','''''')+''','+@colname+')-1'
select @cursor = @cursor +',['+keyName+']' from #key select @cursor = @cursor +' from ' +@tablename+' '+@whereStr --为游标的提取生成语句
select @fetch='',@insert='',@where=''
select @fetch = @fetch +'declare @'+keyName +' sql_variant '+char(13) from #key
select @fetch = @fetch +'fetch next from #c into @rpPtr,@rpPostion'
select @fetch = @fetch +',@'+keyName +char(13) from #key --把游标提取出来的值放到临时表的语句
select @insert = @insert +' insert into #key(keyName,keyValue ) '
select @insert = @insert + ' select '''+replace(keyName,'''','''''')+''',@'+keyName+' union all' +char(13) from #key
select @insert = left(@insert,len(@insert)-10)
select @fetch=@fetch +@insert
--每次获得游标行对应text指针的语句
select @where =' where 1=1'
select @where =@where +' and ['+keyName+']= (select keyValue from #key where keyName = '''+replace(keyName,'''','''''')+''')' from #key
--改写的sql
select @replaceExec = 'updatetext '+@tablename+'.'+@colname+' @rpPtr @rpPostion @rpLen @NewStr' --检查是否存在目标的sql
select @checkExec =' select @rpPostion = PATINDEX ('''+replace(@oldStr,'''','''''')+''','+@colname+')-1 from ' +@tablename+@where --声明游标
exec sp_executesql @cursor
truncate table #key
open #c --提取第一行
exec sp_executesql @fetch,N'@rpPtr varbinary(16) output,@rpPostion int output',@rpPtr output,@rpPostion output
--print @fetch
while @@fetch_status=0
begin
--只要@rpPostion>0 证明还需要替换
while @rpPostion>0
begin
--替换
exec sp_executesql @replaceExec,N' @rpPtr varbinary(16),@rpPostion int,@rpLen int,@newStr nvarchar(512)',@rpPtr,@rpPostion,@rpLen,@NewStr --重新判断是否需要替换
exec sp_executesql @checkExec,N'@rpPostion int output',@rpPostion output
end
truncate table #key --提取下一行
exec sp_executesql @fetch,N'@rpPtr varbinary(16) output,@rpPostion int output',@rpPtr output,@rpPostion output
end
close #c
deallocate #c
if object_id('tempdb..#key') is not null
drop table #key
set nocount off
end go
csdn realgz
(http://topic.csdn.net/u/20080505/20/d2dffbbe-6d6b-41cf-b29a-41149540eafa.html?seed=562172452)if object_id('sp_replaceTextWithMultiColPk') is not null
drop proc sp_replaceTextWithMultiColPk
go create procedure sp_replaceTextWithMultiColPk
@tableName sysname,@colName sysname,@oldStr nvarchar(512),@newStr nvarchar(512),@whereStr nvarchar(200)=''
/*
为一个表内的text做统一替换的存储过程
缺点:慢,而且事实上还没办法支持任意表,而且代码看起来难受
因为 varchar的长度有限,当然还可以扩展,不过在sql 2005 里已经没这个问题了,懒得再写。
realgz 2008-05-05 */
as
begin
set nocount on
declare @cursor nvarchar(4000),@fetch nvarchar(4000),@insert nvarchar(4000),@where nvarchar(4000)
declare @replaceExec nvarchar(4000),@checkExec nvarchar(4000)
declare @rpPtr varbinary(16),@rpPostion int,@rpLen int
if object_id('tempdb..#key') is not null
drop table #key --获得主键列表
select sc.name keyName into #key from
sysobjects so
join sysindexes idx on so.parent_obj=idx.id
join sysindexkeys idk on so.parent_obj=idk.id and idx.indid = idk.indid
join syscolumns sc on so.parent_obj=sc.id and idk.colid=sc.colid
where so.xtype='PK' and so.parent_obj =object_id(@tableName) and idx.status & 0x800>0
if @@rowcount < 1
begin
raiserror ('表不符合要求或者没有这个表',12,1)
return
end
--增加键值列
alter table #key add keyValue sql_variant
alter table #key add primary key(keyName) --替换的长度
select @rpLen=len(@oldStr),@oldStr='%'+@oldStr+'%' --游标声明语句
select @cursor ='declare #c cursor static for select textptr('+@colname+'),PATINDEX ('''+replace(@oldStr,'''','''''')+''','+@colname+')-1'
select @cursor = @cursor +',['+keyName+']' from #key select @cursor = @cursor +' from ' +@tablename+' '+@whereStr --为游标的提取生成语句
select @fetch='',@insert='',@where=''
select @fetch = @fetch +'declare @'+keyName +' sql_variant '+char(13) from #key
select @fetch = @fetch +'fetch next from #c into @rpPtr,@rpPostion'
select @fetch = @fetch +',@'+keyName +char(13) from #key --把游标提取出来的值放到临时表的语句
select @insert = @insert +' insert into #key(keyName,keyValue ) '
select @insert = @insert + ' select '''+replace(keyName,'''','''''')+''',@'+keyName+' union all' +char(13) from #key
select @insert = left(@insert,len(@insert)-10)
select @fetch=@fetch +@insert
--每次获得游标行对应text指针的语句
select @where =' where 1=1'
select @where =@where +' and ['+keyName+']= (select keyValue from #key where keyName = '''+replace(keyName,'''','''''')+''')' from #key
--改写的sql
select @replaceExec = 'updatetext '+@tablename+'.'+@colname+' @rpPtr @rpPostion @rpLen @NewStr' --检查是否存在目标的sql
select @checkExec =' select @rpPostion = PATINDEX ('''+replace(@oldStr,'''','''''')+''','+@colname+')-1 from ' +@tablename+@where --声明游标
exec sp_executesql @cursor
truncate table #key
open #c --提取第一行
exec sp_executesql @fetch,N'@rpPtr varbinary(16) output,@rpPostion int output',@rpPtr output,@rpPostion output
--print @fetch
while @@fetch_status=0
begin
--只要@rpPostion>0 证明还需要替换
while @rpPostion>0
begin
--替换
exec sp_executesql @replaceExec,N' @rpPtr varbinary(16),@rpPostion int,@rpLen int,@newStr nvarchar(512)',@rpPtr,@rpPostion,@rpLen,@NewStr --重新判断是否需要替换
exec sp_executesql @checkExec,N'@rpPostion int output',@rpPostion output
end
truncate table #key --提取下一行
exec sp_executesql @fetch,N'@rpPtr varbinary(16) output,@rpPostion int output',@rpPtr output,@rpPostion output
end
close #c
deallocate #c
if object_id('tempdb..#key') is not null
drop table #key
set nocount off
end go
http://js.fixdown.com/soft/8324.htm
http://www.yiii.net/app/servlet/net.yiii.club.DownloadServlet?Information_Id=I00023471
解压缩密码 www.heibai.net http://www.ttdown.com/softview_8647.htm
注册机产生的是注册码,是两个 用解压缩密码解开后,压缩包里也有一个注册机的 打开log explorer file=> attach log file-> 选择服务器和登陆方式-> connect->
选择数据库-> attach-> 左面对话框中browse-> view log-> 就可以看到log记录了
想恢复的话: 右键log记录 undo transation-> 选择保存文件名和路径-> 然后打开该文件到查询分析器里执行
T-sql代码就可以了 例如 如果log是delete table where ...的话,生成的文件代码就是insert table ....
Log Explorer for SQL Server v3.3 特别版
www.chinaz.com
Log Explorer is the leading transaction analysis and data recovery solution for Microsoft SQL Server. By providing unprecedented access to the SQL Server transaction log, Log Explorer gives you the ability to understand and solve elusive database problems by browsing the transaction log, exporting data to create reports, and selectively recovering modified, deleted, dropped, or truncated data.
打开log explorer file=> attach log file-> 选择服务器和登陆方式-> connect->
选择数据库-> attach-> 左面对话框中browse-> view log-> 就可以看到log记录了
想恢复的话: 右键log记录 undo transation-> 选择保存文件名和路径-> 然后打开该文件到查询分析器里执行
T-sql代码就可以 例如 如果log是delete table where ...的话,生成的文件代码就是insert table .... 右键log记录 undo transation-> 选择保存文件名和路径-> 然后打开该文件到查询分析器里执行T-sql代码 log explorer使用的一个问题 1)对数据库做了完全 差异 和日志备份
备份时选用了删除事务日志中不活动的条目
再用Log explorer打试图看日志时
提示No log recorders found that match the filter,would you like to view unfiltered data
选择yes 就看不到刚才的记录了
如果不选用了删除事务日志中不活动的条目
再用Log explorer打试图看日志时,就能看到原来的日志 2)修改了其中一个表中的部分数据,此时用Log explorer看日志,可以作日志恢复 3)然后恢复备份,(注意:恢复是断开log explorer与数据库的连接,或连接到其他数据上,
否则会出现数据库正在使用无法恢复)
恢复完后,再打开log explorer 提示No log recorders found that match the filter,would you like to view unfiltered data
选择yes 就看不到刚才在2中修改的日志记录,所以无法做恢复. --------------------------------------------------------------------------------
系统中有一个数据库DB1,系统正在使用的过程中,不小心删除了某表T1的数据
现我可以按照用完整备份文件加日志备份时间点TIME1恢复的方法来恢复该数据库 但问题出在该数据库一直还必须在使用中,所以除了你删除了数据的TA表外,其它的
表的数据你不能只是恢复到错误时间点TIME1处,因为,TIME1以后也有数据变化 解决这个问题有两个方法
1:用LOG EXEPLORE 可以轻松搞定 2:先建立一个数据库DB2,将完整备份文件恢复到DB2中,再用DB2中的T1数据来更新
DB1中的T1数据即可.
--查看备份信息
RESTORE FILELISTONLY FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\db1_backup ' --还原旧的备份
RESTORE DATABASE db2 FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\db1_backup ' with replace,norecovery,
move 'db1_Data ' to 'C:\Program Files\Microsoft SQL Server\MSSQL\data\db2_data.mdf ',
move 'db1_Log ' to 'C:\Program Files\Microsoft SQL Server\MSSQL\data\db2_log.ldf ' --还原数据日志到时间点
restore log db2 from disk= 'db_log_backup ' with stopat= '2003/4/22 9:57 '