Click here to Skip to main content
15,887,421 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I am working with application in which i want my values updated every month,but should be based on some calculations.if i create job for the same then i would apply update logic in that job.my question is where to apply logic for calculation?
Posted
Comments
ssd_coolguy 19-Feb-13 4:51am    
do your calculation in stored procedure and execute it through SQL JOB. :)
Anurag Sinha V 19-Feb-13 4:59am    
Yeah, the simplest process will be to call a procedure in a step of the SQL job where the procedure has the updation logic...

regards

hi,

you can write the calculation in job properties->Step->then edit the command and write the update query with calculation.
 
Share this answer
 
Hi fillow the steps below:

1.Open SSMS and a new query window
2.Copy and execute the script below:

USE [msdb]
GO

/****** Object:  Job [UpdateMyCalculation]    Script Date: 2/19/2013 4:36:20 PM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]]    Script Date: 2/19/2013 4:36:20 PM ******/
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'UpdateMyCalculation', 
		@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 [UpdateTable]    Script Date: 2/19/2013 4:36:21 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'UpdateTable', 
		@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'UPDATE Test SET Col1=Col*100 WHERE Col1>100', 
		@database_name=N'TestAyncDB', 
		@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'ScheExe_Occur_Once_In_a_Month', 
		@enabled=1, 
		@freq_type=16, 
		@freq_interval=1, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=1, 
		@active_start_date=20130219, 
		@active_end_date=99991231, 
		@active_start_time=0, 
		@active_end_time=235959, 
		@schedule_uid=N'408c319f-8a01-487d-be63-074c1342f986'
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


3. Edit the following section
@command=N'UPDATE Test SET Col1=Col*100 WHERE Col1>100',


and put your SQL statement instead of "UPDATE Test SET Col1=Col*100 WHERE Col1>100"

4. Execute the script, a Job will be created and this job will run once in a month.
5. If you need to change the schedule (1 month) then
5.1 Go to SQL Server Agent
5.2 Right click on Job
5.3 Choose Manage Schedule
5.4 Select "ScheExe_Occur_Once_In_a_Month" and Click on "Properties"

Enjoy!!!!!
 
Share this answer
 
v2
 
Share this answer
 
Comments
asgharmalik 20-May-14 7:44am    
fbfd

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900