Showing posts with label below. Show all posts
Showing posts with label below. Show all posts

Sunday, March 11, 2012

Changing schedules for a Job outside SQL Server

Below is the script of a Job called "eFIMS_SendEmail" that I wish to
run. The intention is that every day of the week the job will execute
a SPROC at timed intervals. For example, the SundayRun schedule will
run once every 1 hours from 00:30:00 to 23:59:59

However, the clients have stated that they want an interface to this to
enable them easily to change the start time and frequency interval for
each day. It's an easy matter for me to paint them a form from within
the target application to enable the user to enter the start time and
interval for each day. I can then pass these as parameters to a SPROC.
How can I use these values to change the schedules for the job? For
example, if wanted to change SundayRun from once every 1 hours to once
every 30 mins? I know I could do it the hard way, by using string
manipulation (e.g. find string 'SundayRun', then look for the next
occurrence of @.active_start_time, @.freq_subday_type,
@.freq_subday_interval etc. and do some replacement) but this seems
somewhat tricky.

Many thanks

Edward

-- Script generated on 8/31/2006 9:27 AM
-- By: sa
-- Server: BISMARK

BEGIN TRANSACTION
DECLARE @.JobID BINARY(16)
DECLARE @.ReturnCode INT
SELECT @.ReturnCode = 0
IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name =
N'[Uncategorized (Local)]') < 1
EXECUTE msdb.dbo.sp_add_category @.name = N'[Uncategorized (Local)]'

-- Delete the job with the same name (if it exists)
SELECT @.JobID = job_id
FROM msdb.dbo.sysjobs
WHERE (name = N'eFIMS_SendEmail')
IF (@.JobID IS NOT NULL)
BEGIN
-- Check if the job is a multi-server job
IF (EXISTS (SELECT *
FROM msdb.dbo.sysjobservers
WHERE (job_id = @.JobID) AND (server_id <0)))
BEGIN
-- There is, so abort the script
RAISERROR (N'Unable to import job ''eFIMS_SendEmail'' since there
is already a multi-server job with this name.', 16, 1)
GOTO QuitWithRollback
END
ELSE
-- Delete the [local] job
EXECUTE msdb.dbo.sp_delete_job @.job_name = N'eFIMS_SendEmail'
SELECT @.JobID = NULL
END

BEGIN

-- Add the job
EXECUTE @.ReturnCode = msdb.dbo.sp_add_job @.job_id = @.JobID OUTPUT ,
@.job_name = N'eFIMS_SendEmail', @.owner_login_name = N'sa', @.description
= N'No description available.', @.category_name = N'[Uncategorized
(Local)]', @.enabled = 0, @.notify_level_email = 0, @.notify_level_page =
0, @.notify_level_netsend = 0, @.notify_level_eventlog = 2,
@.delete_level= 0
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback

-- Add the job steps
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobstep @.job_id = @.JobID,
@.step_id = 1, @.step_name = N'SendEmail', @.command = N'EXEC
stpSendEmailConfirmation', @.database_name = N'194-eFIMS', @.server =
N'', @.database_user_name = N'', @.subsystem = N'TSQL',
@.cmdexec_success_code = 0, @.flags = 4, @.retry_attempts = 0,
@.retry_interval = 1, @.output_file_name = N'', @.on_success_step_id = 0,
@.on_success_action = 1, @.on_fail_step_id = 0, @.on_fail_action = 2
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback
EXECUTE @.ReturnCode = msdb.dbo.sp_update_job @.job_id = @.JobID,
@.start_step_id = 1

IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback

-- Add the job schedules
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobschedule @.job_id = @.JobID,
@.name = N'SundayRun', @.enabled = 1, @.freq_type = 8, @.active_start_date
= 20060831, @.active_start_time = 3000, @.freq_interval = 1,
@.freq_subday_type = 8, @.freq_subday_interval = 1,
@.freq_relative_interval = 0, @.freq_recurrence_factor = 1,
@.active_end_date = 99991231, @.active_end_time = 235959
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobschedule @.job_id = @.JobID,
@.name = N'MondayRun', @.enabled = 1, @.freq_type = 8, @.active_start_date
= 20060831, @.active_start_time = 3000, @.freq_interval = 2,
@.freq_subday_type = 4, @.freq_subday_interval = 30,
@.freq_relative_interval = 0, @.freq_recurrence_factor = 1,
@.active_end_date = 99991231, @.active_end_time = 235959
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobschedule @.job_id = @.JobID,
@.name = N'TuesdayRun', @.enabled = 1, @.freq_type = 8, @.active_start_date
= 20060831, @.active_start_time = 4000, @.freq_interval = 4,
@.freq_subday_type = 4, @.freq_subday_interval = 40,
@.freq_relative_interval = 0, @.freq_recurrence_factor = 1,
@.active_end_date = 99991231, @.active_end_time = 235959
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobschedule @.job_id = @.JobID,
@.name = N'WednesdayRun', @.enabled = 1, @.freq_type = 8,
@.active_start_date = 20060830, @.active_start_time = 3000,
@.freq_interval = 8, @.freq_subday_type = 4, @.freq_subday_interval = 30,
@.freq_relative_interval = 0, @.freq_recurrence_factor = 1,
@.active_end_date = 99991231, @.active_end_time = 235959
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobschedule @.job_id = @.JobID,
@.name = N'ThursdayRun', @.enabled = 1, @.freq_type = 8,
@.active_start_date = 20060831, @.active_start_time = 3500,
@.freq_interval = 16, @.freq_subday_type = 4, @.freq_subday_interval = 35,
@.freq_relative_interval = 0, @.freq_recurrence_factor = 1,
@.active_end_date = 99991231, @.active_end_time = 235959
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobschedule @.job_id = @.JobID,
@.name = N'FridayRun', @.enabled = 1, @.freq_type = 8, @.active_start_date
= 20060831, @.active_start_time = 3000, @.freq_interval = 32,
@.freq_subday_type = 4, @.freq_subday_interval = 30,
@.freq_relative_interval = 0, @.freq_recurrence_factor = 1,
@.active_end_date = 99991231, @.active_end_time = 235959
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobschedule @.job_id = @.JobID,
@.name = N'SaturdayRun', @.enabled = 1, @.freq_type = 8,
@.active_start_date = 20060831, @.active_start_time = 0, @.freq_interval =
64, @.freq_subday_type = 8, @.freq_subday_interval = 1,
@.freq_relative_interval = 0, @.freq_recurrence_factor = 1,
@.active_end_date = 99991231, @.active_end_time = 235959
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback

-- Add the Target Servers
EXECUTE @.ReturnCode = msdb.dbo.sp_add_jobserver @.job_id = @.JobID,
@.server_name = N'(local)'
IF (@.@.ERROR <0 OR @.ReturnCode <0) GOTO QuitWithRollback

END
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@.@.TRANCOUNT 0) ROLLBACK TRANSACTION
EndSave:teddysnips@.hotmail.com wrote:

Repeat after me - "Look in BOL before posting; Look in BOL before
posting"

- sorry to waste all your time!

Edward

Thursday, February 16, 2012

Changing file names using xp_cmdshell

My goal is to run a script like the one below but for every time I run it I get a new year. For instance if I run it this year I'll get 2004.txt for my file name and next year I'll get 2005.txt for my file name. Is this possible? If so any suggestions.
declare @.firstyr int,
@.var sysname,
@.cmd sysname
select @.firstyr = YEAR ( getdate() )
SET @.var = 'c:\temp\' + convert (varchar(4), @.firstyr)
select @.var
SET @.cmd = @.var + '.txt'
select @.cmd
exec master.dbo.xp_cmdshell 'osql -Sserver -Usa -Ppass -o @.cmd -Q" set nocount on;select top 20 OrderId, CompanyName, OrderDate from Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId = C.CustomerId order by OrderDate desc"' , no_outpu
t
You can do that with dynamic sql, something like this:
declare @.firstyr int,
@.var varchar(1000),
@.cmd varchar(1000),
@.dcmd varchar(1000)
select @.firstyr = YEAR ( getdate() )
SET @.var = 'c:\temp\' + convert (varchar(4), @.firstyr)
select @.var
SET @.cmd = @.var + '.txt'
select @.cmd
--set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -Sserver -Usa -Ppass -o '
+ rtrim(@.cmd) + ' -Q" set nocount on;select top 20 OrderId, CompanyName,
OrderDate from Northwind.dbo.Orders O join Northwind.dbo.Customers C on
O.CustomerId = C.CustomerId order by OrderDate desc"'' , no_output'
set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -S. -E -o ' + rtrim(@.cmd) +
' -Q" set nocount on;select top 20 OrderId, CompanyName, OrderDate from
Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
C.CustomerId order by OrderDate desc"'' , no_output'
print @.dcmd
exec (@.dcmd)
----
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"D" <D@.discussions.microsoft.com> wrote in message
news:E7C149D3-BA9D-4CC0-A669-EA0C3EAA0633@.microsoft.com...
> My goal is to run a script like the one below but for every time I run it
I get a new year. For instance if I run it this year I'll get 2004.txt for
my file name and next year I'll get 2005.txt for my file name. Is this
possible? If so any suggestions.
> declare @.firstyr int,
> @.var sysname,
> @.cmd sysname
> select @.firstyr = YEAR ( getdate() )
> SET @.var = 'c:\temp\' + convert (varchar(4), @.firstyr)
> select @.var
> SET @.cmd = @.var + '.txt'
> select @.cmd
> exec master.dbo.xp_cmdshell 'osql -Sserver -Usa -Ppass -o @.cmd -Q" set
nocount on;select top 20 OrderId, CompanyName, OrderDate from
Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
C.CustomerId order by OrderDate desc"' , no_output
>
>
|||That worked. Thanks for your help. Final example below>>>
declare @.firstyr int,
@.var varchar(1000),
@.cmd varchar(1000),
@.dcmd varchar(1000)
select @.firstyr = YEAR ( getdate() )
SET @.var = 'c:\temp\' + convert (varchar(4), @.firstyr)
select @.var
SET @.cmd = @.var + '.txt'
select @.cmd
set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -Sserver -E -o ' + rtrim(@.cmd) + ' -Q" set nocount on;select top 20 OrderId, CompanyName, OrderDate from Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId = C.CustomerId order by OrderDa
te desc"'' , no_output'
print @.dcmd
exec (@.dcmd)
"Gregory A. Larsen" wrote:

> You can do that with dynamic sql, something like this:
> declare @.firstyr int,
> @.var varchar(1000),
> @.cmd varchar(1000),
> @.dcmd varchar(1000)
> select @.firstyr = YEAR ( getdate() )
> SET @.var = 'c:\temp\' + convert (varchar(4), @.firstyr)
> select @.var
> SET @.cmd = @.var + '.txt'
> select @.cmd
> --set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -Sserver -Usa -Ppass -o '
> + rtrim(@.cmd) + ' -Q" set nocount on;select top 20 OrderId, CompanyName,
> OrderDate from Northwind.dbo.Orders O join Northwind.dbo.Customers C on
> O.CustomerId = C.CustomerId order by OrderDate desc"'' , no_output'
> set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -S. -E -o ' + rtrim(@.cmd) +
> ' -Q" set nocount on;select top 20 OrderId, CompanyName, OrderDate from
> Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
> C.CustomerId order by OrderDate desc"'' , no_output'
> print @.dcmd
> exec (@.dcmd)
> --
> ----
> ----
> --
> Need SQL Server Examples check out my website at
> http://www.geocities.com/sqlserverexamples
> "D" <D@.discussions.microsoft.com> wrote in message
> news:E7C149D3-BA9D-4CC0-A669-EA0C3EAA0633@.microsoft.com...
> I get a new year. For instance if I run it this year I'll get 2004.txt for
> my file name and next year I'll get 2005.txt for my file name. Is this
> possible? If so any suggestions.
> nocount on;select top 20 OrderId, CompanyName, OrderDate from
> Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
> C.CustomerId order by OrderDate desc"' , no_output
>
>

Changing file names using xp_cmdshell

My goal is to run a script like the one below but for every time I run it I
get a new year. For instance if I run it this year I'll get 2004.txt for my
file name and next year I'll get 2005.txt for my file name. Is this possib
le? If so any suggestions.
declare @.firstyr int,
@.var sysname,
@.cmd sysname
select @.firstyr = YEAR ( getdate() )
SET @.var = 'c:\temp\' + convert (varchar(4), @.firstyr)
select @.var
SET @.cmd = @.var + '.txt'
select @.cmd
exec master.dbo.xp_cmdshell 'osql -Sserver -Usa -Ppass -o @.cmd -Q" set noco
unt on;select top 20 OrderId, CompanyName, OrderDate from Northwind.dbo.Orde
rs O join Northwind.dbo.Customers C on O.CustomerId = C.CustomerId order by
OrderDate desc"' , no_outpu
tYou can do that with dynamic sql, something like this:
declare @.firstyr int,
@.var varchar(1000),
@.cmd varchar(1000),
@.dcmd varchar(1000)
select @.firstyr = YEAR ( getdate() )
SET @.var = 'c:\temp' + convert (varchar(4), @.firstyr)
select @.var
SET @.cmd = @.var + '.txt'
select @.cmd
--set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -Sserver -Usa -Ppass -o '
+ rtrim(@.cmd) + ' -Q" set nocount on;select top 20 OrderId, CompanyName,
OrderDate from Northwind.dbo.Orders O join Northwind.dbo.Customers C on
O.CustomerId = C.CustomerId order by OrderDate desc"'' , no_output'
set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -S. -E -o ' + rtrim(@.cmd) +
' -Q" set nocount on;select top 20 OrderId, CompanyName, OrderDate from
Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
C.CustomerId order by OrderDate desc"'' , no_output'
print @.dcmd
exec (@.dcmd)
----
----
--
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"D" <D@.discussions.microsoft.com> wrote in message
news:E7C149D3-BA9D-4CC0-A669-EA0C3EAA0633@.microsoft.com...
> My goal is to run a script like the one below but for every time I run it
I get a new year. For instance if I run it this year I'll get 2004.txt for
my file name and next year I'll get 2005.txt for my file name. Is this
possible? If so any suggestions.
> declare @.firstyr int,
> @.var sysname,
> @.cmd sysname
> select @.firstyr = YEAR ( getdate() )
> SET @.var = 'c:\temp' + convert (varchar(4), @.firstyr)
> select @.var
> SET @.cmd = @.var + '.txt'
> select @.cmd
> exec master.dbo.xp_cmdshell 'osql -Sserver -Usa -Ppass -o @.cmd -Q" set
nocount on;select top 20 OrderId, CompanyName, OrderDate from
Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
C.CustomerId order by OrderDate desc"' , no_output
>
>|||That worked. Thanks for your help. Final example below>>>
declare @.firstyr int,
@.var varchar(1000),
@.cmd varchar(1000),
@.dcmd varchar(1000)
select @.firstyr = YEAR ( getdate() )
SET @.var = 'c:\temp' + convert (varchar(4), @.firstyr)
select @.var
SET @.cmd = @.var + '.txt'
select @.cmd
set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -Sserver -E -o ' + rtrim(@.c
md) + ' -Q" set nocount on;select top 20 OrderId, CompanyName, OrderDate fro
m Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId = C.
CustomerId order by OrderDa
te desc"'' , no_output'
print @.dcmd
exec (@.dcmd)
"Gregory A. Larsen" wrote:

> You can do that with dynamic sql, something like this:
> declare @.firstyr int,
> @.var varchar(1000),
> @.cmd varchar(1000),
> @.dcmd varchar(1000)
> select @.firstyr = YEAR ( getdate() )
> SET @.var = 'c:\temp' + convert (varchar(4), @.firstyr)
> select @.var
> SET @.cmd = @.var + '.txt'
> select @.cmd
> --set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -Sserver -Usa -Ppass -o
'
> + rtrim(@.cmd) + ' -Q" set nocount on;select top 20 OrderId, CompanyName,
> OrderDate from Northwind.dbo.Orders O join Northwind.dbo.Customers C on
> O.CustomerId = C.CustomerId order by OrderDate desc"'' , no_output'
> set @.dcmd = 'exec master.dbo.xp_cmdshell ''osql -S. -E -o ' + rtrim(@.cmd)
+
> ' -Q" set nocount on;select top 20 OrderId, CompanyName, OrderDate from
> Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
> C.CustomerId order by OrderDate desc"'' , no_output'
> print @.dcmd
> exec (@.dcmd)
> --
> ----
--
> ----
--
> --
> Need SQL Server Examples check out my website at
> http://www.geocities.com/sqlserverexamples
> "D" <D@.discussions.microsoft.com> wrote in message
> news:E7C149D3-BA9D-4CC0-A669-EA0C3EAA0633@.microsoft.com...
> I get a new year. For instance if I run it this year I'll get 2004.txt fo
r
> my file name and next year I'll get 2005.txt for my file name. Is this
> possible? If so any suggestions.
> nocount on;select top 20 OrderId, CompanyName, OrderDate from
> Northwind.dbo.Orders O join Northwind.dbo.Customers C on O.CustomerId =
> C.CustomerId order by OrderDate desc"' , no_output
>
>