如何安排作业每天运行 SQL 查询?
我需要知道如何使用 SQL Server 代理作业每天运行 SQL 查询,并使用最低限度的所需配置设置。
I need to know how to make a SQL query run daily using a SQL Server Agent job, with minimum required configuration settings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
展开 SQL Server 代理节点并右键单击 SQL Server 代理中的作业节点并选择
“新建作业”
在
'New Job'
窗口中输入作业名称以及'General'< /code> 选项卡。
选择窗口左侧的
“步骤”
,然后单击底部的“新建”
。在
'Steps'
窗口中输入步骤名称,然后选择要运行查询的数据库。将要运行的 T-SQL 命令粘贴到命令窗口中,然后单击
“确定”
。将
单击“新建作业”窗口左侧的
“计划”
菜单,然后输入计划信息(例如每天和时间)。单击
“确定”
- 应该就是这样。(当然您还可以添加其他选项 - 但我想说这是设置和安排工作所需的最低限度)
Expand the SQL Server Agent node and right click the Jobs node in SQL Server Agent and select
'New Job'
In the
'New Job'
window enter the name of the job and a description on the'General'
tab.Select
'Steps'
on the left hand side of the window and click'New'
at the bottom.In the
'Steps'
window enter a step name and select the database you want the query to run against.Paste in the T-SQL command you want to run into the Command window and click
'OK'
.Click on the
'Schedule'
menu on the left of the New Job window and enter the schedule information (e.g. daily and a time).Click
'OK'
- and that should be it.(There are of course other options you can add - but I would say that is the bare minimum you need to get a job set up and scheduled)
我制作了已接受答案中步骤的动画 GIF。这是来自 MSSQL Server 2012
I made an animated GIF of the steps in the accepted answer. This is from MSSQL Server 2012
要在 t-sql 中执行此操作,您可以使用以下系统存储过程来安排日常作业。此示例安排在每天凌晨 1:00。请参阅Microsoft 帮助。
To do this in t-sql, you can use the following system stored procedures to schedule a daily job. This example schedules daily at 1:00 AM. See Microsoft help for details on syntax of the individual stored procedures and valid range of parameters.
使用 T-SQL:
我的工作是执行存储过程。您可以轻松更改
@command
来运行您的sql。Using T-SQL:
My job is executing stored procedure. You can easy change
@command
to run your sql.如果您想要每日备份//以下sql脚本存储在C:\Users\admin\Desktop\DBScript\DBBackUpSQL.sql中,
打开任务计划程序
创建任务->;选择
Triggers
选项卡 选择New
。按钮 选择“每日单选”按钮,
单击“确定”按钮,
然后单击“操作”选项卡,选择“新建”。
按钮放置
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"
-S ADMIN-PC -i"C:\Users\admin\Desktop\ DBScript\DBBackUpSQL.sql"
在程序/脚本文本框中(确保匹配您的文件路径并将双引号路径放入开始 -> 搜索框中,如果找到则单击它并查看备份是否存在或不)-- 上面的路径可以被插入 100 write 90
"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE" -S ADMIN-PC -i
< code>“C:\Users\admin\Desktop\DBScript\DBBackUpSQL.sql”然后单击“确定”按钮,
脚本将按您每天在“触发器”选项卡上选择的时间执行,
享受它...... ......
if You want daily backup // following sql script store in C:\Users\admin\Desktop\DBScript\DBBackUpSQL.sql
open the Task scheduler
create task-> select
Triggers
tab SelectNew
.Button Select Daily Radio button
click
Ok
Buttonthen click
Action
tab Select New.Button Put
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"
-S ADMIN-PC -i"C:\Users\admin\Desktop\DBScript\DBBackUpSQL.sql"
in the program/script text box(make sure Match your files path and Put the double quoted path in start-> search box and if it find then click it and see the backup is there or not)-- the above path may be insted 100 write 90
"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE" -S ADMIN-PC -i
"C:\Users\admin\Desktop\DBScript\DBBackUpSQL.sql"
then click ok button
the Script will execute on time which you select on Trigger tab on daily basis
enjoy it.............
这是示例代码:
Here's a sample code: