我想从.bat文件运行sql查询。我了解如何创建.bat文件和.sql文件,然后从.bat文件中调用.sql文件。我的问题是如何将其输出存储在任何位置,然后将其发送到电子邮件。
我当前的.bat看起来像
osql -S <ServerName> -U <User Name> -P <Password> < mysqlfile.sql
...我的.sql文件运行以下查询
select * from sysssislog where event='onerror' order by starttime desc
感谢您的帮助。
答案 0 :(得分:2)
答案 1 :(得分:0)
我会使用Blat或VBscript来执行此操作。
osql -S <ServerName> -U <User Name> -P <Password> -o Out.txt < mysqlfile.sql
Blat out.txt -to <recipient> -s <subject> -server <ip or hostname of SMTP server:<port>
这是一个创建VBScript的批处理文件。
@echo off
setlocal
if exist "out.txt" del "out.txt"
osql -S Server -U user -P password -o Out.txt < mysqlfile.sql
set vbs_=Email.vbs
set skip=
findstr "'%skip%VBS" "%~f0" > "%vbs_%"
cscript //nologo "%vbs_%"
ping -n 3 localhost>nul
if exist "%vbs_%" del "%vbs_%"
goto :eof
Const ForReading = 1, ForWriting = 2 'VBS
sFile="Out.txt" 'VBS
Set oFso = CreateObject("Scripting.FileSystemObject") 'VBS
Set oFile = oFso.OpenTextFile(sFile, ForReading) 'VBS
Set oMail=CreateObject("CDO.Message") 'VBS
sSchema="http://schemas.microsoft.com/cdo/configuration/" 'VBS
with oMail.Configuration.Fields 'VBS
.Item(sSchema & "sendusing")= 2 'VBS
.Item(sSchema & "smtpserver")= "YourServer" 'VBS
.Item(sSchema & "smtpserverport") = 25 'VBS
.Update 'VBS
end with 'VBS
oMail.Subject="SQL Log" 'VBS
oMail.From="email@domain.com" 'VBS
oMail.To="Sendto@someother.com" 'VBS
oMail.TextBody = oFile.ReadAll 'VBS
oMail.Send 'VBS
oFile.Close 'VBS
set oMail=nothing 'VBS
set oFile = Nothing 'VBS