Excel VBA - 通过宏在 SQL Server 中执行作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11137302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - Executing a job within SQL Server via a macro
提问by Dean McGarrigle
I have a job stored on a database, scheduled to run every day. But its sometimes necessary to want to execute this job at any given time to view up to date data (I'm using SQL Server Management Studio 2008).
我有一个存储在数据库中的作业,计划每天运行。但有时需要在任何给定时间执行此作业以查看最新数据(我使用的是 SQL Server Management Studio 2008)。
The job itself simply takes data from a view which contains live data and puts it into a table which will then be used as a data source for an excel file. Executing the job drops and re-creates the table with fresh data.
作业本身只是从包含实时数据的视图中获取数据并将其放入一个表中,然后该表将用作 Excel 文件的数据源。执行作业会删除并使用新数据重新创建表。
In excel (2010), i wish to have a 'button' which which pressed will execute the job and then hitting refresh on the data tab in excel will then update the data on the sheet with the fresh data.
在 excel (2010) 中,我希望有一个“按钮”,按下该按钮将执行作业,然后在 excel 中的数据选项卡上点击刷新,然后将使用新数据更新工作表上的数据。
My question is: How do i execute this job from an excel macro?
我的问题是:我如何从 excel 宏执行这项工作?
采纳答案by Alex Dn
You create a SP that moves data from view to table. Then modify the Job that it executes that SP by schedule. Then in Excel Macro you can just use that SP to update the data.
您创建了一个将数据从视图移动到表的 SP。然后修改它按计划执行该 SP 的作业。然后在 Excel 宏中,您可以使用该 SP 来更新数据。
Or see examplehow to run the Job from VBScript
或查看示例如何从 VBScript 运行作业
回答by Andomar
Private Sub CmdRunJob_Click()
Dim con As Object
Set con = CreateObject("ADODB.Connection")
con.Open = "DRIVER={SQL Server};SERVER=YourServer;" & _
"USER=YourUser;PASSWORD=YourPassword;"
con.Execute "exec msdb.dbo.sp_start_job 'YourJob'"
End Sub
回答by Sudhakar B
You can use SQLDMO.SQLServer to execute your job.
您可以使用 SQLDMO.SQLServer 来执行您的作业。