当我们需要重命名多个文件时,如果能够批量重命名文件,就能够大大提高工作效率。那么SQL Server数据库如何对文件进行批量重命名呢?本文我们通过一个代码示例来介绍这个过程,过程如下:

1. 新建自定义函数:从***一个特定字符开始截取字符串
- create function fn_GetLastCharIndex(@Chars varchar(1000),@Char varchar(10))
 - returns varchar(1000)
 - begin
 - declare @i int
 - set @i=CharIndex(@Char,@Chars)
 - while(@i>0)
 - begin
 - set @Chars=Substring(@Chars,@i+1,len(@Chars))
 - set @i=CharIndex(@Char,@Chars)
 - end
 - return @Chars
 - end
 
2. 获取文件名列表
- if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#temp'))
 - drop table #temp
 - create table #temp(filenames varchar(1000))
 - Insert into #temp(filenames)
 - Exec master..xp_cmdshell 'dir F:\灌篮高手-全国大赛全彩版'
 
3. 重命名
- declare @FileName varchar(1000),
 - @NewFileName varchar(1000),
 - @s varchar(1000)
 - declare cur_1 cursor for
 - select dbo.fn_GetLastCharIndex(filenames,' ') FileName from #temp where Right(filenames,4) in ('.jpg') and filename
 - s like '%slamdunk%' order by 1
 - open cur_1
 - fetch cur_1 into @FileName
 - while(@@fetch_status=0)
 - begin
 - set @NewFileName = Replace(@FileName,dbo.fn_GetLastCharIndex(@FileName,'-'),Right('00'+dbo.fn_GetLastCharIndex(@FileName,'-'),7))
 - set @s='ren F:\灌篮高手-全国大赛全彩版\' + @FileName + ' ' + @NewFileName
 - Exec master..xp_cmdshell @s,no_output
 - fetch cur_1 into @FileName
 - end
 - deallocate cur_1
 
上述了代码就描述了SQL Server数据库对文件的批量重命名的过程,希望能够带给您一些收获。本文就介绍到这里,谢谢大家的浏览!
                文章名称:一个SQLServer数据库文件批量重命名的代码示例
                
                转载来于:http://www.csdahua.cn/qtweb/news17/470267.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网