批量执行文件夹下面的所有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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL Server中,可以使用多种方法来批量执行SQL脚本。以下是几种常用的方法: 1. SQL Server Management Studio (SSMS):SSMS是SQL Server的官方管理工具,它提供了一个查询编辑器,可以在其中编写和执行SQL脚本。你可以打开一个新的查询窗口,将多个SQL语句粘贴到该窗口中,并执行这些语句。 2. SQLCMD 实用程序:SQLCMD是SQL Server提供的一个命令行工具,可以用于执行SQL脚本。你可以创建一个文本文件,将多个SQL语句保存在其中,然后使用SQLCMD命令来执行该文件。例如,可以使用以下命令执行脚本文件: ``` sqlcmd -S <服务器名称> -d <数据库名称> -U <用户名> -P <密码> -i <脚本文件路径> ``` 其中,`<服务器名称>`是SQL Server实例的名称,`<数据库名称>`是要执行脚本的数据库名称,`<用户名>`和`<密码>`是连接数据库所需的凭据,`<脚本文件路径>`是包含SQL语句的文件路径。 3. PowerShell:如果你熟悉PowerShell脚本语言,也可以使用PowerShell来批量执行SQL脚本。你可以使用`Invoke-Sqlcmd` cmdlet来连接到SQL Server并执行SQL语句。以下是一个示例: ```powershell $server = "<服务器名称>" $database = "<数据库名称>" $username = "<用户名>" $password = "<密码>" $scriptPath = "<脚本文件路径>" $connectionString = "Server=$server;Database=$database;User ID=$username;Password=$password;" $sqlScript = Get-Content $scriptPath | Out-String Invoke-Sqlcmd -ServerInstance $server -Database $database -Username $username -Password $password -InputFile $scriptPath ``` 在上面的示例中,你需要将`<服务器名称>`、`<数据库名称>`、`<用户名>`、`<密码>`和`<脚本文件路径>`替换为实际的值。 这些方法可以根据你的需求选择使用。如果你有其他相关问题,请告诉我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值