Create SQL Server Agent job by using existing job script? - sql-server

I have two sql server agent jobs: Job1 and Job2. Since Job2 is very similar to Job1, I right-clicked on Job1 > Script Job As > Create To and used that script to create the Job2.
Now I'm seeing that any change I make in the schedule of Job2 is also affecting Job1, and I'm assuming that it's happening because both have the same #schedule_uid.
So, two questions:
Is it correct to generate a job by using the sql script of another job? If it's correct, how can I fix this error where changes made in one job affect the other job?
Thanks.

Schedules are distinct objects within SQL Server and as you have found are independent to the Job, referenced by an ID.
If you are creating jobs via script you just need to either not assign a schedule, create a new schedule as well for every job, or define a set of possible schedules that fit around your requirements/maintenance windows and specify the correct ID in your script.
Obviously any two jobs that share a schedule will be affected if you change the schedule so, if you foresee a lot of individual job management/tweaking going on, it may be best that your script creates a schedule and then reference that new schedule in the job creation.

If you look in your script you will see that the #schedule_uid is the same. If you would fetch the new #schedule_uid (and all other hardcoded id's) from the tables in the MSDB database you will get a correct running job

Related

How to make the entries delete automatically after certain time in Potgresql

I have created a database and it is going to have some temporary values such as login type and some hask keys that I want it to be deleted after certain period of time.
I have installed postgres on docker.
I found out a solution through pgagent, but I don't know if it works on docker. Can anyone help me figure out how to do it?
Thanks in advance
You can create the function/procedure to delete the records based on the criteria.
Schedule the execution of the function either using the cron job or the pgagent to execute at the specified time. For executing on the cron job you need to create the shell scrip to call from the cron job.

Running several SQL jobs simultaneously under one job

Is it possible in SQL Server to run several jobs simultaneously under different sessions under same job.
For example, I have N stored procedures to run. They all have to be run under different sessions and start at the same time. I don't want to create N jobs, I want all of them start at the same time under 1 job.
In the past I've had one job create and start several other jobs using the sp_add_job command. If you set the delete level to 3 then the job will then get automatically deleted once it has completed.
The disadvantages are security and monitoring all the jobs.
I don't see any other option than using ssis sql script tasks for different scripts without any link between them and executing them. This will allow to run different SP or sql script to run parallel.Thanks!

Need to split two SQL Server Agent Jobs into different schedules

I have two SQL Agent jobs that share the same schedule due to an error I made during the creation of the second job. I generated a script in SSMS and edited some values, but I left the schedule_uid the same. Now it turns out that while those two jobs are running at the same time, they are corrupting each other's data.
What I need to do is leave the original job alone, but create a new schedule and have the second job use this new schedule. However, my searches for the correct way to do this have all resulted in dead-ends.
None of this can be done using a UI .. it all must be scripted so it can be run during a maintenance window without me present.
Thanks in advance for any assistance.
Use msdb.dbo.sp_detach_schedule followed by msdb.dbo.sp_add_jobschedule.

Best Way to Create Nightly Replication of Sql Server Database

I want to create a job that runs every night. I have a database (MyDatabase) that I want to copy/replace my staging database with (MyDatabase_Stage).
I presume the easiest way is to do something related to SQL Server Agent, but I have never done anything like this before. What is the best practice and easiest route to go to get this setup and tested?
I do not care if the data is 24 hours old and the most important criteria is that is does a full copy every night at the same time.
copy the .bak file to your staging server and restore from there using a script.
Run the script on a schedule in an agent job.
The benefit of a script is that you can add functionality later - for instance you might not require audit tables and these can be truncated.
Check out snapshot replication. As part of the setup, it'll create a SQL Agent job to do the copy of the data and whatnot. You can then schedule that job at whatever time and frequency you like.

Execute long running jobs in SQL Server

I am working with SQL Server 2008. Using the Agent, I have created a job and scheduled it to execute every minute.
The job executes a stored procedure that moves data from table XXX, to a temp table, and then eventually into table YYY.
The execution of the job may take more than one minute - since the data is rather large.
Will a second instance of the job be started even though the first instance is still running?
If so, should I mark records in temp table (status = 1) to indicate that those records are being processed by a previous instance of the job?
Is there a way for me to check that an instance of the job is currently running, so that I don't initiate a second instance of the job?
Is there another solution for this that I am unaware of? (throughput is important)
Only one instance of a particular job can run at any one time.
So there is no need to take any particular precautions against another execution of the same job beginning before the first one has stopped.
check this post
How to Prevent Sql Server Jobs to Run simultaneously
How to Prevent Sql Server Jobs to Run simultaneously
As Well HERE
Running Jobs
http://technet.microsoft.com/en-us/library/aa213815(v=sql.80).aspx
If a job has started according to its schedule, you cannot start another instance of that job on the same server until the scheduled job has completed. In multiserver environments, every target server can run one instance of the same job simultaneously.

Resources