SQL Server 2008 Agent jobs. Two procedures and a view - sql

I'm using two stored procedures a view to create report.
Every 3 months, I have to update these two stored procedures and view automatically.
First, I should update stored procedure 1
Then, I need to update view (it takes data from stored procedure 1)
Finally, I need to update stored procedure 2 (it takes data form stored procedure 1 and from the view)
To do this, I would like to use SQL Server Agent jobs. Is it possible to run one SQL Server Agent job and updated these two store procedure and view in that sequence? or should I create SQL Server Agent job with command to update stored procedure 1 then I need to create another job with command to update view and then create another job with command to stored procedure 2.
Thank you for any suggestions.

Yes, the tasks that are described in the question can be done with one single SQL Server Agent Job.
For this SQL Server Agent Job you may want to set several steps
- Step 1: run SP1
- Step 2: update view
- Step 3: run SP2)
OR actually create only one step that will have all the actions inside. Also, the job should have the 3 months schedule created as needed.
Note: you refer as "update stored procedure 1" .. I think you actually mean "execute stored procedure 1".
For further documentation I recommend: http://msdn.microsoft.com/en-us/library/ms190268(v=sql.105).aspx

Related

Can I run multiple instances of the same stored procedure that includes temp tables in SQL Server

I'm trying to execute a SQL Server stored procedure through Python Pyodbc and to get the selection results printed out into .csv files. But this procedure is currently being used in other daily tasks so I'm worried that if my executing the procedure in python will interrupt the daily scheduled job process in SQL Server Agent. In the procedure, it creates several temporary tables #temp_a, #temp_b, and #temp_c. I'm wondering if these temp tables will break the scheduled jobs that include this procedure since there might be other procedures that will be creating temp table names using the same name such as #temp_a or #temp_b. The temp tables are created inside the procedure but with no delete query written. I could have tested this myself but the database I'm working on right now is just so fragile that I was told not to create tests. Thanks!
Yes
The temp-Tables will be created per Session.
I got the procedure execute by different Sessions on the same time there will be the same count of the temp-tables as sessions executed.
This temp-tables have each a different name:
#V_...._000000003EB1
#V_...._000000003EB8
The example above are the temp-Tables created by the same Procedure executed two times by different sessions at the same time.
So your scenario couldn't happen

Can I write a SQL script to dynamically create all stored procedures and views from one database to another?

Can I write a SQL script to dynamically create all stored procedures and views from one database to another?:
I have 2 instances of MyDatabase
1 MyDatabase instance on ServerX
1 MyDatabase instance on ServerY
I'd like to write a SQL script which does the following:
Drop all stored procedures and views on ServerY
Generate CREATE statements for all stored procedures and views on ServerX
Execute those CREATE statements on ServerY, so all stored procedures and views on ServerY match those on ServerX
I'm sure this can be done but can anyone here describe a way to go about doing this?
There is an easier way to do this - SQL Server can script the creation of the objects for you into a single *.sql file. You then just run that script on the other server. You can even have it include the data from the existing database. For a detailed walk through, see: https://dzone.com/articles/generate-database-scripts-with-data-in-sql-server
Why? SQL Server has all this built in for you
Once all your SP and views are on Server.. Right click on your database and click Tasks > Generate scripts. SQL Server Management Studio is able to generate the CREATE scripts for you, which can be done on SP, views and more.
Then you simple copy this script and execute it on ServerX server/database.
BUT if you need it to be automated you should use powershell to simulate this task. Doing this in a SQL script isn't the best solution.
Create a link from the server X to the server Y and select, assume server x for the primary.

Execute Stored Procedure on every month-end

I have written a stored procedure where data with-in a particular data range will be extracted and dump into a temp table. I want this procedure to be called by running a .bat file and prompt user to input From and To dates. Any approach I can use?
Is it important which user executed that procedure ? If it is not you can create scheduled Sql Server agent job.

Running synchronous commands to between two sql servers

I'm running a stored procedure on server1 from my application. The stored procedure does a bunch of stuff and populate a table on server2 with the result from the procedure.
I'm using linked server to accomplish this.
When the stored procedure is done running the application continues and tries to do some manipulation of the result from the stored procedure.
My problem is that the results from the stored procedure has not been completely inserted into the tables yet, so the manipulation of the tables fails.
So my question is. Is it possible to ensure the insert into on the linked server is done synchronous? I would like to have the stored procedure not return until the tables on the linked server actually is done.
You can use an output parameter of the first procedure. When the table is create on the second server the output parameter value will be return to your application and indicates the operation is ready.
If the things are difficult then this you can try setting a different isolation level of your store procedure:
http://msdn.microsoft.com/en-us/library/ms173763.aspx
I found the reason for this strange behavior. There was a line of code in my stored procedure added during debug that did a select on a temporary mem table before the data in the same table was written to the linked server.
When the select statement was run, the control was given back to my application and at the same time the stored procedure continued running. I guess the stored procedure was running synchronously from the start.

How to run a stored procedure automatically every day

How do I set up to run a stored procedure automatically every day in SQL Server 2008 R2?
Set up a SQL job http://msdn.microsoft.com/en-us/library/ms135739.aspx
You need to use the Job scheduler in the sql agent. Sql express doesn't include it so I just have a batch file run as a scheduled task to run it.
-James
Was discussed here:
Scheduled run of stored procedure on SQL server
Under SQL agent you need to go to the job scheduler and create a job that runs the stored proc. Once you have created the job you can create one or more schedules for the job. http://msdn.microsoft.com/en-us/library/ms135739.aspx