Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql脚本运行程序_Sql_Sql Server_Powershell_Scripting - Fatal编程技术网

Sql脚本运行程序

Sql脚本运行程序,sql,sql-server,powershell,scripting,Sql,Sql Server,Powershell,Scripting,当我使用上述命令从文件夹中运行一批脚本时,如果中间脚本中仍然存在问题(如中间的create/Alter/DROP DML脚本),那么它应该只在那里停止,并需要给我一条错误消息。您需要做几件事: 将ErrorActionPreference设置为停止 将-b参数与sqlcmd.exe实用程序一起使用 捕获并记录或显示sqlcmd.exe实用程序的输出 我和我在这里重新发布了答案: Get-ChildItem ".\Stored Procedures\*.sql" | ForEach-Object

当我使用上述命令从文件夹中运行一批脚本时,如果中间脚本中仍然存在问题(如中间的create/Alter/DROP DML脚本),那么它应该只在那里停止,并需要给我一条错误消息。

您需要做几件事:

  • 将ErrorActionPreference设置为停止
  • 将-b参数与sqlcmd.exe实用程序一起使用
  • 捕获并记录或显示sqlcmd.exe实用程序的输出
  • 我和我在这里重新发布了答案:

    Get-ChildItem ".\Stored Procedures\*.sql" | ForEach-Object { sqlcmd -S ServerName -d     DatabaseName -E -i $_.FullName }
    
    echo "select 'Good 1'" > C:\temp\scripts\1.sql
    echo "select * from missingTable" > C:\temp\scripts\2.sql
    echo "Select 'Good 3'" > C:\temp\scripts\3.sql
    
    $ErrorActionPreference = "Stop"
    
    ForEach ($S In Gci -Path "C:\Temp\Scripts\" -Filter *.sql | Sort-Object Name) {
        try { 
            $result = SqlCmd -b -S $env:computername\sql1 -i $S.FullName
            $result = $result -join "`n"
    
            if ($LASTEXITCODE -ne 0) {
                throw "$S.FullName : $lastexitcode : $result"
            }
            else {
                write-output "Success: $($s.fullname) : $result" | Out-File C:\Temp\Scripts\sqllogging.txt -Append
            }
        }
        catch {
            write-output "Failed: $_ " | Out-File C:\Temp\Scripts\sqllogging.txt -Append
            throw
        } 
    }