ITPub博客

首页 > 数据库 > SQL Server > powershell 批量循环更新sql脚本

powershell 批量循环更新sql脚本

原创 SQL Server 作者:alexlu232100 时间:2016-12-06 16:41:04 0 删除 编辑
当你遇到几十个sql要一次性更新的,而且这些sql 有先后顺序,开发不愿意给整理到一起的时候,只能自己动手了.

Add-PSSnapin -Name SqlServerCmdletSnapin100
Add-PSSnapin -Name SqlServerProviderSnapin100


$ScriptPath="E:\update"
$db_name="dbname"
$db_instance_add="127.0.0.1,1433"
[System.IO.DirectoryInfo]$DirectoryInfo=New-Object System.IO.DirectoryInfo $ScriptPath | Sort-Object
try {
foreach( $f In ($DirectoryInfo.GetFiles("*.sql")))
{
      $ScriptList=$($ScriptPath+"\"+$f.Name)
      Invoke-Sqlcmd -ServerInstance "$db_instance_add" -Database "$db_name" -InputFile "$ScriptList" -ErrorAction Stop
      Write-Host $f.Name  " ...OK!"
    }
Write-Host "CommitTransaction ."
  }
catch {
      Write-Host $_.Exception.toString()
Write-Host $HOSTNAME $f.Name  " ...ERROR!"
        }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/20630340/viewspace-2129878/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2013-04-27

  • 博文量
    6
  • 访问量
    12108