简体   繁体   English

计划Microsoft SQL Server 2008 R2中的存储过程

[英]Schedule Stored Procedure in Microsoft SQL Server 2008 R2

I have to run a stored procedure on a daily basis at a particular time. 我必须每天在特定时间运行存储过程。 I have struggled with Windows Service and after unsuccessful attempts I started looking into schedule a SQL job or schedule an event. 我一直在努力使用Windows Service,在尝试失败之后,我开始考虑安排SQL作业或安排事件。

In order to schedule a SQL Server job I had to create one using the SQL Server Agent. 为了安排SQL Server作业,我必须使用SQL Server代理创建一个作业。 I am working on Microsoft SQL Server 2008 R2 Express (with Advanced Services) on Windows and could not locate the SQL Server Agent even though I have admin access (logged in as windows authentication). 我正在Windows上使用Microsoft SQL Server 2008 R2 Express(具有高级服务),即使我具有管理员访问权限(以Windows身份验证登录)也无法找到SQL Server代理。

With the given scenario, what is the best way to schedule a stored procedure and how should I go about it? 在给定的情况下,调度存储过程的最佳方法是什么,该如何处理?

There is a one way that you can schedule procedure using another SP. 您可以使用一种方法来计划使用另一个SP的过程。 Create new SP like below: 如下创建新的SP:

CREATE SP1
AS
WHILE (1)
BEGIN
    WAITFOR TIME '22:20';   -- Your Time
    EXECUTE 'Your SP Name'
END;
GO

You can use error handling as per your requirement. 您可以根据需要使用错误处理。

SQL server Agent contains thre back end tables to hold the front end data when you are creating a job SQL Server代理包含三个后端表,用于在创建作业时保存前端数据

sysjobs
sysjobschedules
sysjobsteps

Enter job related information in sysjobs , give scheduling information in schedules table and give your sql code in steps like exec procname. 在sysjobs中输入与作业相关的信息,在schedules表中提供调度信息,并以exec procname之类的步骤提供您的sql代码。 Note that it should be updated in msdb database only. 请注意,仅应在msdb数据库中对其进行更新。

Since SQL Server Express does not include SQL Server Agent, you will not be able to schedule SQL jobs directly in SQL Server. 由于SQL Server Express不包括SQL Server代理,因此您将无法直接在SQL Server中计划SQL作业。 However, that does not mean that you cannot schedule jobs to be run. 但是,这并不意味着您无法计划要运行的作业。

Use sqlcmd . 使用sqlcmd It is a command-line tool. 它是一个命令行工具。 (Run sqlcmd /? in a command window and it will show you the exact command-line syntax.) (在命令窗口中运行sqlcmd /? ,它将向您显示确切的命令行语法。)

For example, you could use the built-in Windows Task Scheduler to schedule and run a specific command-line task using a trusted connection (the -E argument): 例如,您可以使用内置的Windows Task Scheduler通过受信任的连接( -E参数)来调度和运行特定的命令行任务:

sqlcmd -S {Server}\{SqlExpressInstance} -E -d {Database} -Q "EXECUTE {StoredProcedure}"

..Or using specific login credentials and a password (the -U and -P arguments): ..或使用特定的登录凭据和密码( -U-P参数):

sqlcmd -U {LoginId} -P {Password} -S {Server}\{SqlExpressInstance} -d {Database} -Q "EXECUTE {StoredProcedure}"

For more information about using sqlcmd , read: Using the sqlcmd Utility (SQL Server Express) 有关使用sqlcmd更多信息,请阅读: 使用sqlcmd实用程序(SQL Server Express)

For more information about using the Windows Task Scheduler, read: Task Scheduler 有关使用Windows Task Scheduler的更多信息,请阅读: Task Scheduler

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL Server 2008 R2存储过程中获取记录集? - How to get the recordset in SQL Server 2008 r2 stored procedure? 带文字的存储过程参数在SQL Server 2008 R2中不起作用 - Stored procedure parameters with literals not working in SQL Server 2008 R2 如何在SQL Server 2008 R2中加快存储过程 - How to speed up stored procedure in SQL Server 2008 R2 SQL Server 2008 R2中运行缓慢的存储过程 - Slow running stored procedure in SQL Server 2008 R2 SQL Server 2008 R2中具有多个选择和插入操作的存储过程的性能问题 - Performance issue of stored procedure with several select and insert operations in SQL Server 2008 R2 如何获取SQL Server 2008 R2中从python调用的存储过程返回的值 - How to get the value returned by a stored procedure called from python in SQL server 2008 R2 带FORMSOF和同义词库的存储过程参数(SQL Server 2008 R2 SP2) - Stored Procedure Parameter with FORMSOF and Thesaurus (SQL Server 2008 R2 SP2) SQL Server 2008 R2:从存储过程返回结果并插入临时表 - SQL Server 2008 R2: Return result from stored procedure and insert into temp table 在SQL Server 2008 R2中存储过程中存储多个数据集? - Store multiple data sets from stored procedure in SQL Server 2008 R2? SQL Server 2008 R2:具有一个或多个表名称的存储过程作为参数 - SQL Server 2008 R2: stored procedure with one or more table names a parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM