Execute Stored Procedure on every month-end - sql

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.

Related

Is there any way to a script inside stored procedure in SQL Server?

I know it's a weird scenario. But I need to execute a script inside a stored procedure in SQL Server.
I've created a SQL script to create a test table and populate it with some of the data from the original table. This script is in a separate file.
I also updated some stored procedures to use test mode. I created a parameter called #IsTestMode in the stored procedures.
If #IsTestMode is true, it checks whether the test table exists or not. If the test table does not exist, It should run that SQL script to create the test table and populate it with the data from the original table.
Is it possible to execute SQL script (in a separate file) inside a stored procedure?
Instead of storing the script in a file, create a new stored procedure using that code, say dbo.CreateTestData. Then, in your main proc:
CREATE PROC dbo.Whatever
#IsTestMode BIT = 0 /*Default to no*/
AS
BEGIN
IF #IsTestMode = 1
EXEC dbo.CreateTestData;
DoOtherStuff...
END
GO

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

SQL Server 2008 Agent jobs. Two procedures and a view

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

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.

SQL: Using Stored Procedure within a Stored Procedure

I have a few stored procedures that return the same set of data (same columns) to a user. The stored procedure called depends on certain conditions. These stored procedures are fairly intensive and are being run by every user of the system. I would like to create stored procedure that calls each of these procedures and stores the data on a separate table. I will then run this new stored procedure every 5 minutes or so and let the users pull from the new table.
T_OutboundCallList is a permanent table with the same columns as returned by the two stored procedures.
I would like something like the following but when I try to run this it just runs continuously and I have to stop the procedure.
BEGIN
TRUNCATE TABLE T_OutboundCallList
INSERT T_OutboundCallList EXECUTE p_LeadVendor_GetCallsForCallList
INSERT T_OutboundCallList EXECUTE p_CallLog_GetAbandonedCallsCallList
END
Each of the procedures (*CallList) return a list of calls to be made and I do want them entered into the new table in this order (LeadVendor calls before AbandonedCalls). I also need to clear the table before adding the calls as there may be new calls that need to be higher in the list.
Is there some problem with this procedure that I am not seeing?
Thanks,
Brian
Without seeing the code in your *CallList procs it is hard to say what issue you are having. You should have the insert commands inside of your nested procedure. You can use the results of a procedure to insert data, but not like you are above. It is using OPENROWSET, and I think you will be better off the way I suggested.