Solved

MSSQL Scheduled Job

Posted on 2015-02-19
5
76 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select Sum query with group by 8 46
Create a Calendar table 29 43
What is needed to become a DBA? 7 53
File attachment in the SQL Database from application 10 45
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

734 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