我想使用 MS SQL Transact-SQL 来安排运行脚本 C:\script\my_script.sql
每天下午 6 点(周六和周日除外)。
然后,结果保存在文件夹C:\data\xxx.csv
中哪里xxx
是运行脚本的日期,例如2019-2-18
.
这里我想安排一个脚本(或自动备份表),它从sql表中获取完整的数据并将其存储在另一个表中,以当天的日期作为数据库名称,该表可用于查看任何数据记录天。
另外,如何删除计划任务?
我使用了服务器代理,但无法查看文本。
非常感谢。
最佳答案
如果您不想更改现有设置,请使用带有 SQLCMD 的 Windows 任务计划程序..
Open a command prompt window.
In the Command Prompt window, type:
sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt
Press ENTER.
您可以使用与此处相同的解决方案:create Scheduler task to invoke SQLCMD
Your best bet would be to create a Console Application which executes the SQL Command and performs the email.
You can then use setup a task under Task Scheduler to use the Start a program option and run it on the schedule you prefer
编辑:
要摆脱点线,请看这里:
Sqlcmd to generate file without dashed line under header, without row count
To remove column headers you should use -h parameter with value -1. See ref (section Formatting Options).
或者来自这篇文章:
How to export data as CSV format from SQL Server using sqlcmd?
sqlcmd -S MyServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" -o "MyData.csv" -h-1 -s"," -w 700
-h-1 removes column name headers from the result
-s"," sets the column seperator to ,
-w 700 sets the row width to 700 chars (this will need to be as wide as the longest row or it will wrap to the next line)
关于sql-server - 如何安排在SQL Server上运行SQL脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54781521/