Solved

MSSQL Scheduled Job

Posted on 2015-02-19
5
66 Views
Last Modified: 2015-02-21
Hello Experts,

I come from an Oracle background, but have been forced into using SQL Server for some very specific reporting.

I need to be able to schedule a job to refresh a table from a view every night.

In oracle I can do this in 2 minutes. in SQL server, I am completely lost.

I have a query
SELECT * INTO REPORTSMASTERS FROM REPORTSMASTERS_REFRESH

Open in new window


I need the above to be executed every night at 12am.

Right now I am doing this by hand and the task is taking up time i rather not be spending.

Any help would be greatly appreciated.
0
Comment
Question by:FutureDBA-
5 Comments
 
LVL 21

Accepted Solution

by:
Alpesh Patel earned 500 total points
Comment Utility
Hi,

Using SQL Job on SQL Agent you can do that.

Drop Table REPORTSMASTERS;
SELECT * INTO REPORTSMASTERS FROM REPORTSMASTERS_REFRESH;

Put this script in Script area of sql job and schedule it.

http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&sqi=2&ved=0CB0QFjAA&url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-IN%2Flibrary%2Fms187910.aspx&ei=trTmVPiHAZedugSllIKQBA&usg=AFQjCNFcR6IJcBr1-bkrLIvlvfkIUOzwQw&sig2=lpo_IODT1zioEvq9rVR5ag&bvm=bv.86475890,d.c2E
0
 
LVL 6

Expert Comment

by:Mandeep Singh
Comment Utility
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
In SQL Server you have a service called SQL Server Agent that is used to perform jobs (scheduled and non-schedule tasks).
You can use the SQL Server Management Studio (SSMS) and use the GUI to create a new job. It's simple and self-explanatory. You just need to create a job with a step for running the T-SQL command and then schedule it.
Don't forget to provide the correct user since the jobs runs under an user credential context. Also don't forget to start the SQL Server Agent service. I usually set it to Automatic so it will start every time the server is rebooted.
0
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
Run the script bellow on the sever you need to to create your job. Please make sure that you change the <YourDatabase> with the actual database name:
USE [msdb]
GO

/****** Object:  Job [Update REPORTSMASTERS]    Script Date: 20/02/2015 9:25:47 AM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]]    Script Date: 20/02/2015 9:25:47 AM ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'Update REPORTSMASTERS', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=0, 
		@notify_level_netsend=0, 
		@notify_level_page=0, 
		@delete_level=0, 
		@description=N'No description available.', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Update REPORTSMASTERS...]    Script Date: 20/02/2015 9:25:47 AM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Update REPORTSMASTERS...', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_success_step_id=0, 
		@on_fail_action=2, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'Drop Table REPORTSMASTERS;
SELECT * INTO REPORTSMASTERS FROM REPORTSMASTERS_REFRESH;', 
		@database_name=N'<YourDatabase>', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Update REPORTSMASTERS', 
		@enabled=1, 
		@freq_type=4, 
		@freq_interval=1, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=0, 
		@active_start_date=20150220, 
		@active_end_date=99991231, 
		@active_start_time=0, 
		@active_end_time=235959, 
		@schedule_uid=NULL
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

GO

Open in new window


You will be able to see the job in UI if you open the SQL Management Studio, connect to the server and then in the left panel where you can see the server node you do the following:

1. Expand the server node if is not expanded already
2. Right click on the SQL Server Agent  node
3. Expand the Jobs node
4. Right click on the Update REPORTSMASTERS job
5. On the pop-up window click on Properties.

You will now be able to see the elements of the job like the steps(only 1 in this case), schedule and so forth. You can edit them as you need.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now