MSSQL Scheduled Job

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.
FutureDBA-Asked:
Who is Participating?
 
Alpesh PatelConnect With a Mentor Assistant ConsultantCommented:
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
 
Mandeep SinghDatabase AdministratorCommented:
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
ZberteocCommented:
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
All Courses

From novice to tech pro — start learning today.