Groups | Blog | Home
all groups > sql server (alternate) > october 2004 >

sql server (alternate) : Updating tables via agent ?


mpinner NO[at]SPAM tiscali.co.uk
10/14/2004 8:21:52 AM
I have a temporary table that I want to read, update another table
with the information the delete the record from the Temp table every
hour or so.

(The reason for doing this is we have an application that puts data in
table1 this has to be moved to table2 before it can be used - this
movement is done using a trigger but can not delete the entry in
table1 because the record is locked - so I creat the temporary record
and hopr to clean up later)

What is the best way to do this, I was thinking possably an agent ?
does anyone have any sample code to get me started.

Thanks
mpinner NO[at]SPAM tiscali.co.uk
10/15/2004 8:08:19 AM
Thanks Simon -

This is probably obvious to all you experts but how do you schedule a
stored procedure - Doesn't look like you can select the procedure from
SQL Server Agents - Jobs.

Martin

Simon Hayes
10/15/2004 11:21:58 AM

[quoted text, click to view]

I'm not exactly sure of what you need to do, but perhaps something like
this?

-- INSERT rows which don't exist in the target table
insert into t2
(col1, col2, ...)
select col1, col2, ...
from t1
where not exists (select * from t2 where t2.primary_key = t1.primary_key)

-- DELETE rows from the source table which are already in the target
delete from t1
where exists (select * from t2 where t2.primary_key = t1.primary_key)

You can put this into a stored procedure, then schedule the stored procedure
with a scheduled job.

Simon

Simon Hayes
10/15/2004 5:17:10 PM

[quoted text, click to view]

Create a new job, go to the Steps tab, click New and you get a window which
defaults to TSQL step - just put "exec dbo.MyProc" in the Command window.
Also make sure you choose the correct database. After that, go to the
Schedules tab and add a Schedule which executes the command at the time you
want.

The "Implementing Jobs" section in Books Online goes into more detail about
all the options available in the task dialogues.

Simon

AddThis Social Bookmark Button