依次使用powershell和运行时参数调用多个sql
我正在尝试按顺序运行sql查询。如果任何一个sql查询失败,则windows powershell脚本应退出并发送电子邮件。应将日志写入日志目录。其中data=<这将在运行时传递> 示例代码如下:依次使用powershell和运行时参数调用多个sql,sql,sql-server,powershell,Sql,Sql Server,Powershell,我正在尝试按顺序运行sql查询。如果任何一个sql查询失败,则windows powershell脚本应退出并发送电子邮件。应将日志写入日志目录。其中data= 示例代码如下: Invoke-Sqlcmd -Query "SELECT data from emp where data=<run time argument>;" -ServerInstance "MyComputer\MyInstance" Invoke-Sqlcmd -Query "SELECT data fr
Invoke-Sqlcmd -Query "SELECT data from emp where data=<run time argument>;" -ServerInstance "MyComputer\MyInstance"
Invoke-Sqlcmd -Query "SELECT data from class where data=<run time argument>;" -ServerInstance "MyComputer\MyInstance"
Invoke-Sqlcmd -Query "SELECT data from stud where data=<run time argument>;" -ServerInstance "MyComputer\MyInstance"
Invoke-Sqlcmd -Query "SELECT data from cust where data=<run time argument>;" -ServerInstance "MyComputer\MyInstance"
Invoke-Sqlcmd -Query "SELECT data from new where data=<run time argument>;" -ServerInstance "MyComputer\MyInstance"
Invoke Sqlcmd-Query“从emp中选择数据,其中数据=;”-ServerInstance“MyComputer\MyInstance”
调用Sqlcmd-Query“从数据为的类中选择数据”;-ServerInstance“MyComputer\MyInstance”
调用Sqlcmd-Query“从stud中选择数据,其中数据=;”-ServerInstance“MyComputer\MyInstance”
调用Sqlcmd-Query“从cust中选择数据,其中数据=;”-ServerInstance“MyComputer\MyInstance”
调用Sqlcmd-Query“从新数据中选择数据,其中数据=;”-ServerInstance“MyComputer\MyInstance”
任何帮助都将不胜感激
关于,当“sql查询失败”时是什么样子的?您可以依赖Invoke-SqlCmd函数的返回,或者有一条预期的“fail”消息(或多条消息) 我不熟悉调用SqlCmd。退房
-AbortOnError
看起来对您很有帮助,就像-ErrorLevel
一样
下面是一个单一预期错误的概要,以及如何扩展的注释。将查询存储在数组中是值得的,这样您就可以循环并打破循环,而不是使用线性代码(在线性代码中,您必须在每次调用sqlcmd后复制并粘贴检查部分
# string with a single error. you could use an array and add
# a foreach ($error in $errors) on the line marked #here
$expectedError = "Failed"
# Functions have to appear above where they are used
Function Check-SQLResults($result){
# a try-catch statement will execute the code in the try part, going
# to the catach part on a TERMINATING error
try{
# check each line for your expected error
foreach($line in $result){
#here
if($line -like "*$expectedError*"){
Write-Error "Something went wrong: $line" -ErrorAction Stop
}
}
# true is only returned if none of the result lines are like your error
return $true
}catch{
# false is returned if any lines contain error
return $false
}
}
# store the sql outcome in a variable so you can check it
$result = Invoke-Sqlcmd -Query "SELECT data from emp where data=;" -ServerInstance "MyComputer\MyInstance"
# using a function that tells you if the results contain an error or not is neater.
# again, this is manually dealing with errors and invoke-sqlcmd provides other options.
$resultIsErrorFree = Check-SQLResults -result $result
If(resultIsErrorFree -eq $true){
# execute next invoke-sqlcmd
}else{
# Send e-mail. $results can be added to body.
}
到目前为止,您尝试了什么?您是否收集了有关需要整合的不同部分(SQL、电子邮件、写入文件)的任何信息?脚本必须按顺序执行sql查询。将sql查询的输出存储在文件命名中,如file1、file2等。运行时将传递where条件输入。非常感谢。我对powershell非常陌生。
$SqlQuery=“select*from emp where data=”try{$SqlResourceMon=Invoke SqlCmd-Query$SqlQuery-ServerInstance“SQLINSTANCENAME”-数据库“temp”-ErrorAction Stop-verbose 4>&1 | outfile someoutfile.txt}catch{$u发送邮件-从“X”-到“Y”-主题“测试电子邮件”-正文“测试”运行sql$SqlQuery时出错”
请检查此操作是否有效。欢迎使用StackOverflow,欢迎使用PowerShell!MSDN和文档是您最好的朋友。据我所知,代码看起来不错;您是否尝试过运行它?我无法为您运行它。