SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Server Job
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Pepo
Starting Member

Bulgaria
5 Posts

Posted - 08/15/2012 :  01:40:47  Show Profile  Reply with Quote
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

India
48031 Posts

Posted - 08/15/2012 :  10:14:32  Show Profile  Reply with Quote
where's the second step?

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

Go to Top of Page

Pepo
Starting Member

Bulgaria
5 Posts

Posted - 08/17/2012 :  02:22:04  Show Profile  Reply with Quote
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

India
48031 Posts

Posted - 08/17/2012 :  10:12:39  Show Profile  Reply with Quote
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

Bulgaria
5 Posts

Posted - 08/27/2012 :  03:32:13  Show Profile  Reply with Quote
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?

Edited by - Pepo on 08/27/2012 03:32:56
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48031 Posts

Posted - 08/27/2012 :  11:42:15  Show Profile  Reply with Quote
yep..as per your stated requirement

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

Go to Top of Page

Pepo
Starting Member

Bulgaria
5 Posts

Posted - 08/28/2012 :  04:49:58  Show Profile  Reply with Quote
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

India
48031 Posts

Posted - 08/28/2012 :  23:19:26  Show Profile  Reply with Quote
yep...thats enough...

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

Go to Top of Page

Pepo
Starting Member

Bulgaria
5 Posts

Posted - 08/29/2012 :  05:09:01  Show Profile  Reply with Quote
OK,
Thank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48031 Posts

Posted - 08/29/2012 :  12:47:47  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000