sql-server - 如何安排在SQL Server上运行SQL脚本?

标签 sql-server scheduled-tasks

我想使用 MS SQL Transact-SQL 来安排运行脚本 C:\script\my_script.sql每天下午 6 点(周六和周日除外)。

然后,结果保存在文件夹C:\data\xxx.csv中哪里xxx是运行脚本的日期,例如2019-2-18 .

这里我想安排一个脚本(或自动备份表),它从sql表中获取完整的数据并将其存储在另一个表中,以当天的日期作为数据库名称,该表可用于查看任何数据记录天。

另外,如何删除计划任务?

我使用了服务器代理,但无法查看文本。

enter image description here

非常感谢。

最佳答案

如果您不想更改现有设置,请使用带有 SQLCMD 的 Windows 任务计划程序..

看看这里 https://learn.microsoft.com/en-us/sql/ssms/scripting/sqlcmd-run-transact-sql-script-files?view=sql-server-2017

  1. Open a command prompt window.

  2. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt

  3. 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/

相关文章:

java - 使用 Quartz-scheduler 的作业中的自定义对象参数

sql-server - 使用 Key Vault 和证书的 SQL Azure IaaS 加密

sql - 如何将两列或多列合并为一列?

sql - Node.js SQL Server 代码似乎有误,但仍然有效?

php - 在 MSSQL 上获取日期时间

python - 每 X 秒作为用户输入运行 python 脚本

c++ - SCHED_RR 线程上的 Posix 计时器正在使用 100% CPU

mysql - Insert Into Table B Select * From Table A,但在表 B 中有一个额外的列,这可能吗?

使用管理员运行的 Python Windows 任务

java - 如何使用 Timer 类调用方法,做某事,重置计时器,重复?