Solved

MSSQL Scheduled Job

Posted on 2015-02-19
5
81 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 21

Accepted Solution

by:
Alpesh Patel earned 500 total points
ID: 40620455
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
ID: 40620719
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40620836
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 27

Expert Comment

by:Zberteoc
ID: 40621176
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

688 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