Automate execution of multiple SQL Server 2012 scripts in specific order

天大地大妈咪最大 提交于 2019-12-25 04:45:18

问题


I needed some guidance with a task I have never worked with SQL Server 2012. Your help would be very much appreciated. I have inherited a data model in SQL Server.

Basically, I have 5 SQL scripts:

  1. Script A
    1. Script B
    2. Script C
    3. Script D
    4. Script E

For running successfully script B,it needs access to tables generated by script A to perform calculation. Basically, the scripts are feeding each other. I need to run the scripts in a specific order.

My first idea was "stored procedure". So far, I have in the past only written a stored procedure to execute code from the same script that do not require executing other scripts.

My question is, what are some ideas you propose for automatically executing the above 5 scripts in a specific order? how can I do this? What would you recommend me to think when doing this?

Running the complete list of scripts takes around 10 hours.


回答1:


You could easily create a new SQL Server Agent Job task like this:

  1. Expand the SQL Server Agent node and right click the Jobs node in SQL Server Agent and select 'New Job'

  2. In the 'New Job' window enter the name of the job and a description on the 'General' tab.

  3. Select 'Steps' on the left hand side of the window and click 'New' at the bottom.

  4. In the 'Steps' window enter a step name and select the database you want the query to run against.

  5. Paste in the T-SQL command you want to run into the Command window and click 'OK'.

  6. Click on the 'Schedule' menu on the left of the New Job window and enter the schedule information (e.g. daily and a time).

  7. Click 'OK' - and that should be it.

Repeat these steps for each scripts in the order you want them to run. And, there you go!



来源:https://stackoverflow.com/questions/31660580/automate-execution-of-multiple-sql-server-2012-scripts-in-specific-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!