I have two stored procedures where I need to create job based on query execution.
Means first I need to execute a statement based on the result, then I need to execute the stored procedures. And I wish to schedule this job every day.
Make a trigger, its execution body is your 'schedule'. And it is executed when time indicate 00:00:00(means everyday) by timer.
Related
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
I am calling same stored procedure with different parameters.
Inside SP, we are filling data into a table and fetching those records as output of the stored procedure. Is there a chance that there will be an overlap when two persons call the same stored procedure and the records might be wrong.
May I know which stored procedure called first . More over, we are not supposed to use any locking mechanism like the first one should finish before starting execution of the second time execution of sp?
Please explain us the execution plan when the same procedure is called by two different users at the same time.
Term "Critical Section" in general is what you would like to research. Also known as Mutexes/Locking. Learning about those you will be able to create stored procedures that are guaranteed to complete execution without overlapping executions.
I have a stored procedure that every night will be executed by the windows task schedular. I just need to know the following 3 things about the execution of the stored procedure:
1. is the stored procedure executed by the task schedular?
2. Is the stored procedure executed but with errors?
3. Is the stored procedure executed successfully?
I have created a table which will hold this information. The table has two fields: Datetime and Description.
At the end of my stored procedure I have written an insert statement that inserts a record to my table for every run (see code below). But I need to define two inserts (see items 2 and 3 above), 3(Successfully) or 2(with Errors). Number 1 is when the sp is not executed so there is no record inserted to the table. I have used the following statement but it is not working, because when a error accours before the insert to the table, the stored procedure stops, so the insert never happens.
IF ##ROWCOUNT > 0 and ##ERROR = 0
BEGIN
insert into Table1
select GETDATE(), 'Successfully'
END
ELSE
insert into Table1
select GETDATE(), 'With errors'
Do you know a good way to solve this problem? Which statements can I use to save a record the this table?
I am using SQL server 2005.
P.S. This data will be displayed in a report: I will write a query which has to return al the data from the table + the dates which are not existed in the table between a from and to date.
Your procedure should have a TRY/CATCH implementation
http://msdn.microsoft.com/en-us/library/ms175976(v=sql.90).aspx
Wrap the whole procedure logic inside BEGIN TRY ... END TRY and use your CATCH block to write errors in the log table. Successful logs are written from the end of TRY block.
You can also combine with RAISERROR if you need to handle any custom error checking and 'jump' from anywhere inside procedure to the CATCH block.
SQLFiddle DEMO
You can use the administrative task scheduler to execute stored procedures at a specific time. You must first define a task for the stored procedure execution. Then, when the specified time or event occurs for the stored procedure to run, the administrative task scheduler calls the stored procedure.
Specifically, the administrative task scheduler performs the following actions:
The administrative task scheduler connects to the DB2® member that is specified in the task parameter DB2SSID. If the administrative task scheduler cannot establish a connection, it skips execution of the stored procedure and sets the last execution status to the NOTRUN state.
The administrative task scheduler retrieves parameter values for the stored procedure from DB2 by using the SELECT statement that is defined in the task parameter procedure-input. If an error occurs when the administrative task scheduler retrieves those parameter values, the administrative task scheduler:
Does not call the stored procedure.
Sets the last execution status of the task to the error code that is returned by DB2.
The administrative task scheduler issues an SQL CALL statement with the retrieved parameter values and a stored procedure name. The procedure name is concatenated from the task parameters procedure-schema and procedure-name. The SQL CALL statement is synchronous, and the execution thread is blocked until the stored procedure finishes execution. The administrative task scheduler sets the last execution status to the values that are returned by DB2.
The administrative task scheduler issues a COMMIT statement.
The administrative task scheduler closes the connection to DB2.
I have a stored procedure which is supposed to execute at a regular interval to do some heavy background processing on the backend. The amount of data the stored procedure has to deal with is variable.
I intend to set up the stored procedure as a scheduled job.
Because the processing must be done sequentially, I need to ensure only one instance of the stored procedure runs at any time.
Given how heavy the data is, it is possible that the scheduled job may activate another instance before the first one has had time to complete.
My question is: how does one check for other instances of the stored procedure and abort if one exists already?
Create semaphore table to set flags and check for them in your procedure.
I want to measure the execution time (using I guess duration from SQL Server Profiler) of an insert statement that has an instead-of insert trigger on it. How do I measure the complete time of this statement including the trigger time?
The execution time (duration) that you see in SQL server profiler for a query is the time it took to execute that query including evaluating any triggers or other constraints.
Because triggers are intended to be used as an alternative way to check data integrity, an SQL statement is not considered to have completed until any triggers have also finished.
Update: An overview of some commonly used SQL Server profiler events:
SQL:BatchCompleted Occurs when a SQL server batch (a group of statements) has completed execution - the duration is the total time to execute the batch.
SQL:StmtCompleted Occurs when a SQL statement executed as part of a batch completes execution - again the duration is the time to execute that single statement.
SP:Completed Occurs when a stored procedure has completed execution - the duration shown is the time to complete execution of the stored procedure.
SP:StmtCompleted Occurs when an SQL statement executed as part of a stored procedure completes.
A batch is a set of SQL statements separated by a GO statement, however to understand the above you should also know that all SQL server commands are executed in the context of a batch*.
Also, each of the above events also has a corresponding Starting event - SP:Starting, SQL:BatchStarting, SQL:StmtStarting and SP:StmtCompleted. These don't list durations (as we don't know the duration yet because its not completed, however do help show when the duration recording starts from).
To better understand the relationship between these events, I recommend that you experiment with capturing some traces of some simple examples (from within SQL Server Management Studio), for example:
SELECT * FROM SomeTable
GO
SELECT * FROM SomeTable
SELECT * FROM OtherTable
GO
SELECT * FROM SomeTable
exec SomeProc
GO
As you should see, for each of the 3 examples above you always get a SQL:BatchStarting and SQL:BatchCompleted, the other event types however provide more detail on the individual commands run.
For this reason I generally tend to use the SQL:BatchCompleted event the most, however if the statement you are attempting to measure is executed as part of a larger batch (or in a stored procedure) then you may find one of the other event classes helpful.
See TSQL Event Category (MSDN) for more information on the various SQL Server Profiling events - there are lots!
Finally, if you are executing this command from within SQL Server Management studio, be aware that the simplest way to record the execution time is to use the client side statistics feature:
(*) I'm pretty sure that everything is executed as part of a batch, although I've not managed to find any evidence on the internet to confirm this.