Run a Job in SQL Server

Introduction


When we think about a job in SQL Server then "SQL Server Agent" comes to our mind. Now one more question comes to our mind is Why we need a SQL Job in SQL Server?
Lets understand why exactly Job is?

Job is nothing but a continuous process which help to do some work based upon the schedule set by the user.
It simply automate the process as per the command.

In SQL Server we can create job using a tool called called Sql Server Agent which used to create job and do the automation based upon the command.

Get Started

Step - 1
Lets create a table as below.

create table tbl2(id int primary key identity(1,1),employeeName varchar(20),place varchar(40),addres varchar(20))

Step - 2

Suppose I wanted to insert a record to the database every 10 second with out user interaction.
How an i do this????

Yes, we can do this with the help of a job in sql server by using "Sql server Agent".

Lets understand how can we achieve this using Sql server agent?

See the below screenshot.

Fig-1

See the highlighted section. Where we can save our jobs . You can see "MyTestJob" which is created earlier.

Let create a new job . For that follow the below steps.

Go to "Sql Server Agent" --> New--> Job 
See the below screenshot.
Fig-2



Once you click on Job as shown in the figure then a popup window will open.

Fig-3

In general side menu give the details like Name of your job and its description as described above.

Then go to 2nd  option called Steps. Then Click New below the popup.

Then you can find a screen as below.
Fig-4


Fill the details as shown in the screenshot.


Then Return back as shownin Fig-3. Then Go to "Schedules" . And click New. Then fill the details as below.



Here The Date and time schedule for the job. This means in How many sec the job will trigger. 
Here we given 10 sec.

Lets click Ok. Again click OK to the window shown in Fig-3.

Lets start the SQL Server Agent if it has not started . To do that Right click on it and click Start.

Similarly to start the Job you can Right click the Job--> Click on "Start Job At Step".
Then You can see a record will insert to the table each 10 seconds.

Step - 3

Lets check whether our job is running or not..... 

Under "SQL Server Agent" we can find a tool called "Job Activity Monitor"  as below.



Right click it and click "View Job Activity".
Then a new popup will show which described about your job whether your job is "Enabled or Disabled" ,"Last job start time","Next schedule time" and much more.....

Conclusion

Basically Job is used to Automate the process which simplify the extra work and avoid to be error prone.



Thanks.............

Share:

No comments:

Post a Comment

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts