How to calculate the next run date for a SQL Server agent job - sql

I am developing a custom system for scheduling task & I am following Microsoft SQL Server Agent approach to schedule task.
I have replicated sysschedules table for capturing users scheduling occurrences and timings.
Now I need to know how SQL Server get the Next_run_date and time from this table.
I have found a solution on the internet which are using sysjobhistory table to get the next run date, but in my case, I have an only sysschedules table. So, what is the formula to calculate the next_run_date and time for any job?
Note: I am looking for entire code, concept/algorithm will work for me.

Related

Hyperion Reporting - Query for prior day (DB2 and Oracle)

I am using Hyperion Reporting Studio. I have a report where I want to calculate the turn around time for messages that come in to my department.
I need to find a way, whether it's custom SQL or just a feature for the report to always pull the data from the prior day. I have an Open_Date filter where the setting is > 06/06/16 12:00 AM.
However I will always need the date to be the day prior to the current one. I will be using EPM which allows you to setup recurring reports, that run then get emailed to you on a daily basis, automatically. I need to figure out some custom SQL Hyperion can use in my date field and have not found any solutions.
Additional info: Using Hyperion Interactive Reporting Studio; DB2 and Oracle Databases.
It depends on your backend (DB2 or Oracle) which syntax you use. Also, do you want "yesterday" relative to the user, or to the server? Assuming the latter, because this sounds like a job on the server.
I think what you're looking for is:
CURRENT DATE for DB2 and
SYSDATE for Oracle
These are the equivalent of "today" relative to the server's date and time. Will the job run after midnight? It might be as simple as adding -1 but you could run into trouble if the job runs before midnight sometimes, and after midnight other times (don't know what would happen if the job ran through midnight).

Creating EVENTS in SQL Server

I want to execute a query at a specific time. In MySQL we use events for that, example is as follows:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
Please let me know how to do the same in SQL Server. Thanks.
PS : Sorry if its a repeated question. I tried searching for some time but didn't get it in SQL Server
You can use Sql server agent for this task and you can schedule there. Its easy way.
Another one is "waitfor time" check this link http://msdn.microsoft.com/en-IN/library/ms187331.aspx
(and you need to create a trigger with while loop logic and there wait for time is useful)
Another one create a bat file and schedule that in windows scheduler (if you are using sql sever express edition)

View the waiting/suspended queries between a time period in the past

I am trying to optimize an SQL process using the dmv ([sys].[dm_os_wait_stats]).
Is there any way that we can see the waiting/suspended queries between a time period in the past?. Like want to have records only from 3pm today.
Currently I clean the instance every time before running the process using
DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR);
GO
I suggest that using monitoring tools such as Idera or Redgate monitor in order to monitor sql server waiting. You can also copy ([sys].[dm_os_wait_stats]) data in other table periodically.

create ms sql server job schedule from datetime database

I'm using microsoft SQL server management studio 2005
I'm trying to run a job at a specific time and that time is stored in a database.
I can't insert the schedule manually because it is up to the user to decide what date and time the job(s) has to be done. php collects the time and date and sends it to a database.
I thought about runing a job every min and in my execution I have an if statement that only activates when the datetime stored in the database is one minute greater or lower than the current datetime. but doing it like this would be inaccurate and very inefficient. would it be possible to create a schedule directly from a query or job?
You can create and modify both jobs and schedules programmatically using SMO or TSQL. That means you can use an external application, a stored procedure or a trigger to create or update a job schedule based on the table data. SMO is probably the best way to go if you want to build an application to manage jobs, but on the other hand it's .NET only.

Begin and monitor progress on long running SQL queries via ajax

Is it possible to start a query that will take a significant amount of time and monitor progress from the UI via Ajax?
I considered starting the process as a "run once" job that is scheduled to run immediately. I could store the results in a temporary table for quick retrieval once it's complete. I could also log the run time of the report and average that out, to guestimate the running time for the progress bar.
I use Microsoft SQL 2005 at the moment, but I'm willing to other DBMS such as SQL 2008, MySQL, etc if necessary.
One idea, if the long running job populates another table.
You have a 2nd database connection to monitor how many rows are processed out of the source rows, and show a simple "x rows processed" every few second
SELECT COUNT(*) FROM TargetTable WITH (NOLOCK)
If you have a source table too:
SELECT COUNT(*) FROM SourceTable WITH (NOLOCK)
..then you can use "x of y rows processed"
Basically, you have to use a 2nd connection to monitor the first. However, you also need something to measure...