批量执行文件夹下面的所有sql脚本

drop function f_split
go

/*****************************************************************
--PROGRAMMER:EDWARD
--PURPOSE:批量执行sql脚本,批量执行文件夹下面的所有sql脚本
--DATETIME:2006-11-1
*****************************************************************/
if exists(select * from sysobjects where name='exeSqlFileBatch' and type='P')
begin
drop proc exeSqlFileBatch
end
go
create procedure exeSqlFileBatch
(
@returnCode int=0 output, --返回错误数
@returnSucceed int=0 output, --成功执行文件数量
@serverName varchar(1000), --服务器名称
@dataBase varchar(500), --数据库名称
@uid varchar(50), --用户名
@pwd varchar(50), --密码
@filePath varchar(5000) --文件路径
)
with encryption as
declare @strsql varchar(6000)
set @strsql=' dir '+@filePath+'\*.tab'

--创建临时表
create table #tmptb(fileDes varchar(5000))
--将文件夹信息插入临时表
insert into #tmptb
exec master.dbo.xp_cmdshell @strsql

if @@error <>0
begin
set @returnCode=1
return @returnCode
end

--从临时表中提取有用的记录
select dbo.f_split(fileDes,' ')as fileDes
into #tmepUse from #tmptb
where fileDes like '%.tab'

--定义游标
declare fetFileName cursor
for select fileDes from #tmepUse

declare @strFileName varchar(2000)
declare @reExe int

--定义错误信息表
create table #errorTable(errorFileName varchar(1000))


--打开游标并历遍进行取值
open fetFileName
fetch next from fetFileName into @strFileName
while @@FETCH_STATUS = 0
begin
--执行该数据脚本文件 【OSQL命令行执行查询工具】[-S服务器][-U登录标识符][-P口令][-d使用数据库名][-i输入文件][][]
set @strsql='osql -S '+@serverName
+' -U '+ @uid +' -P '+@pwd +' -d '+@dataBase +' -i '
+@filePath +'\'+@strFileName
exec @reExe=master.dbo.xp_cmdShell @strsql
--统计执行错误数执行成功数
if @reExe=0
set @returnSucceed=@returnSucceed+1
else
begin
--没办法,xp_cmdShell 总返回零。可能无法获取osql所发生的错误
insert #errorTable values(@strFileName)
set @returnCode=@returnCode+1
end

fetch next from fetFileName into @strFileName
end

close fetFileName
deallocate fetFileName

--返回错误信息
select *from #errorTable

return


--测试--------------------------------------------------------------------------
--测试准备
--测试前请先创建函数
/*-------------------
--实现split功能 的函数
--只返回最后的一串
*/
if exists(select * from sysobjects where name='f_split' and type='FN')
begin
drop function f_split
end
go
create function f_split(
@SourceSql varchar(8000),--字符串
@StrSeprate varchar(10)--分隔符
)
returns varchar(1000)
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
return @SourceSql
end


--恢复系统存储过程XP_CMDSHELL
use master
go
sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll'
go
--sql2005下启用XP_CMDSHELL
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO



--测试
declare @returnCode int
declare @returnSucceed int
set @returnCode=0
set @returnSucceed=0
set nocount on
exec exeSqlFileBatch @returnCode output,@returnSucceed output,'.','test11','sa','sa','E:\QPCT系统v2.0\SERVER\SQL_SPACE_JZX'
print @returnCode
print @returnSucceed

冠亿科技,尽心尽力
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值