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

来源:互联网 发布:公安部人口信息数据库 编辑:程序博客网 时间:2024/05/18 13:10

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

原创粉丝点击