Updating tables via agent ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Pinner

    Updating tables via agent ?

    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
    Martin
  • Simon Hayes

    #2
    Re: Updating tables via agent ?


    "Martin Pinner" <mpinner@tiscal i.co.uk> wrote in message
    news:46108a36.0 410140721.2fcae 0ed@posting.goo gle.com...[color=blue]
    >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
    > Martin[/color]

    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


    Comment

    • Martin Pinner

      #3
      Re: Updating tables via agent ?

      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" <sql@hayes.ch > wrote in message news:<416f96b6$ 1_3@news.bluewi n.ch>...

      Comment

      • Simon Hayes

        #4
        Re: Updating tables via agent ?


        "Martin Pinner" <mpinner@tiscal i.co.uk> wrote in message
        news:46108a36.0 410150708.69b59 40@posting.goog le.com...[color=blue]
        > 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" <sql@hayes.ch > wrote in message
        > news:<416f96b6$ 1_3@news.bluewi n.ch>...[/color]

        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 "Implementi ng Jobs" section in Books Online goes into more detail about
        all the options available in the task dialogues.

        Simon


        Comment

        Working...