如何在不使用 GUI 的情况下创建 SQL Server 代理?
我正在使用 Angel LMS 及其构建在 SQL Server 平台上。我相信是 2005 年,但对此并不能 100% 确定。
不管怎样,也许我的伪代码会阐明这个答案。另外,字符串必须使用单引号,并且连接字符为 +。
我需要每天运行 2-5 次(频率尚未确定,但你明白了)。这是事务块/伪代码:
BEGIN TRANSACTION
BEGIN TRY
<select statement>
<update statement>
<delete statement>
<insert statement>
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
DECLARE @Msg NVARCHAR(MAX)
SELECT @Msg=ERROR_MESSAGE()
RAISERROR('Error Occured: %s', 20, 101,@msg) WITH LOG
END CATCH
我对数据库的唯一访问是运行 sql 命令的文本框。我可以创建和删除表,运行事务块,显然还可以选择/插入/更新/删除。我找不到任何创建代理的命令,只能找到使用企业管理器或任何 GUI 名称时的步骤。
此外,一些有关如何操纵代理的时间安排的参考也会有所帮助。当我测试它时,我想将其设置为每十五分钟左右运行一次。
EDIT
EXEC dbo.sp_add_job @job_name=N'test' returned the error
Could not find stored procedure 'dbo.sp_add_job'.
I am using Angel LMS and its built on a SQL Server platform. I believe its 2005, but not 100% sure about that.
Anyway, maybe my pseudo-code will shed light on that answer. Also single quotes must be used for strings and the concatenate character is +.
I need to have something run 2-5 times a day (the frequency is not determined yet, but you get the point). Here's the transaction block/pseudo-code:
BEGIN TRANSACTION
BEGIN TRY
<select statement>
<update statement>
<delete statement>
<insert statement>
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
DECLARE @Msg NVARCHAR(MAX)
SELECT @Msg=ERROR_MESSAGE()
RAISERROR('Error Occured: %s', 20, 101,@msg) WITH LOG
END CATCH
My only access to the database, is a text box that runs sql commands. I can create and drop tables, run transactions blocks, and obviously select/insert/update/delete. I can't find any commands to create an agent, but only the steps if you are using the Enterprise Manager or whatever the GUI is called.
Also, some reference on how to manipulate the timing schedule of the agent would help out. When I test it, I want to set it to run every fifteen minutes or so.
EDIT
EXEC dbo.sp_add_job @job_name=N'test' returned the error
Could not find stored procedure 'dbo.sp_add_job'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要在 SQL Server Agent 中创建 SQL 作业(假设您有权限),则必须创建以下内容:
1) 作业本身
2)作业中运行SQL代码的一个步骤
3) 运行时间的时间表。
为此,需要以下存储过程(工作示例):
至于存储过程本身,您需要在此处查看语法:
sp_add_job
sp_add_jobstep
sp_add_jobschedule
这有点棘手,但这应该能让你继续下去。
If you need to create a SQL job in SQL Server Agent (assuming you have rights), you have to create the following:
1) The job itself
2) A step in the job to run the SQL code
3) A schedule of when to run it.
The following stored procedures are necessary to do this (working example):
As for the sprocs themselves, you'll want to check out the syntax here:
sp_add_job
sp_add_jobstep
sp_add_jobschedule
It's a little tricky, but that should get you going.
首先,您需要确定 SQL Server 是否是 Express 版本,因为 SQL Express 没有 SQL Agent 功能。
在命令窗口中,运行以下命令:
结果将包含有关版本的信息。
如果您有 Express,则需要找到另一种方式来运行命令 - 可能使用 SQLCMD 和 Windows 调度程序,或者 SQL Express 中 SQL 代理的几个第三方替代品之一 - 如果您 Google“sql Express 代理”,您会找到几个。我没有使用它们的经验,所以我无法推荐。
如果您有标准版或企业版,则可以找到通过 T-SQL 创建 SQL 代理作业的步骤 此处。
First off, you need to establish whether the SQL Server is Express Edition, since SQL Express has no SQL Agent functionality.
In your command window, run the following command:
The result will include information on the edition.
If you have Express, you will need to find another way to run your commands - possibly using SQLCMD and the windows scheduler, or one of the several third party replacements for the SQL Agent in SQL Express - if you Google "sql express agent", you'll find several. I've no experience with them, so I can't recommend one.
If you have Standard or Enterprise editions, the steps to create SQL Agent jobs through T-SQL can be found here.