Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Server Job

Author  Topic 

Pepo
Starting Member

5 Posts

Posted - 2012-08-15 : 01:40:47
Hello.

I want to make a server job, that every day copy values from one table to other. I wrote a script, but I want to use it in two databases on one server. Do I need to create the other job or can used only one with different steps (where only new will be database name) ? I want to use it in DB1 and DB2.
This is my script, can you tell me that is correct?
Thanks!
EXEC msdb..sp_add_job
@job_name='myJob',
@enabled=1,
@description='Test'
GO

EXEC msdb..sp_add_jobstep
@job_name='myJob',
@step_name='jobStepChangeFuturedAddress',
@subsystem='TSQL',
@command='

UPDATE Users SET Address = t1.Address,City = t1.City
FROM
(SELECT Address,City
FROM Address
where DATEDIFF(dd,0, EffectiveDate) = DATEDIFF(dd,0, GETDATE())) t1
WHERE ID = t1.UserID


',
@database_name='DB1'
GO

EXEC msdb..sp_add_jobschedule
@job_name='myJob',
@name = 'jobScheduleMyJob',
@freq_type=4,
@freq_interval=1,
@active_start_time='000000'
GO

EXEC msdb..sp_add_jobserver
@job_name='myJob'
@server_name='MyServerName'
GO


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-15 : 10:14:32
where's the second step?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Pepo
Starting Member

5 Posts

Posted - 2012-08-17 : 02:22:04
I have (and need) only one step, but need to make it in both database.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-17 : 10:12:39
even then you should have two steps. the step command being exactly same but databases being different

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Pepo
Starting Member

5 Posts

Posted - 2012-08-27 : 03:32:13
If I understand correctly, the only thing I should add is


EXEC msdb..sp_add_jobstep
@job_name='myJob',
@step_name='jobStepChangeFuturedAddress',
@subsystem='TSQL',
@command='

UPDATE Users SET Address = t1.Address,City = t1.City
FROM
(SELECT Address,City
FROM Address
where DATEDIFF(dd,0, EffectiveDate) = DATEDIFF(dd,0, GETDATE())) t1
WHERE ID = t1.UserID


',
@database_name='DB2'
GO


right?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-27 : 11:42:15
yep..as per your stated requirement

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Pepo
Starting Member

5 Posts

Posted - 2012-08-28 : 04:49:58
Thank you a lot!
I have one more question - If I execute all from my first post (now, I want to run this job only for first database) and after few days I want to start this job to my second Database, I only need to execute the jobstop from my previous post, right?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-28 : 23:19:26
yep...thats enough...

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Pepo
Starting Member

5 Posts

Posted - 2012-08-29 : 05:09:01
OK,
Thank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-29 : 12:47:47
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -