Triggers

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

    Triggers

    I have never used triggers before and was wondering what the easiest
    way to perform the following scenario would be:

    I have a table called Courses and a table called Schedule. I would
    like a corresponding record to be created automatcially in the
    Schedule table when a record is added to the Course table.

    What is the best and easiest way to do this?

    Thanks!

  • Ed Murphy

    #2
    Re: Triggers

    t8ntboy wrote:
    I have never used triggers before and was wondering what the easiest
    way to perform the following scenario would be:
    >
    I have a table called Courses and a table called Schedule. I would
    like a corresponding record to be created automatcially in the
    Schedule table when a record is added to the Course table.
    >
    What is the best and easiest way to do this?
    Can the Schedule table later end up containing multiple records
    corresponding to that same Course record? If not, then why are
    they separate tables?

    Comment

    • t8ntboy

      #3
      Re: Triggers

      On Mar 15, 10:46 pm, Ed Murphy <emurph...@soca l.rr.comwrote:
      t8ntboy wrote:
      I have never used triggers before and was wondering what the easiest
      way to perform the following scenario would be:
      >
      I have a table called Courses and a table called Schedule.  I would
      like a corresponding record to be created automatcially in the
      Schedule table when a record is added to the Course table.
      >
      What is the best and easiest way to do this?
      >
      Can the Schedule table later end up containing multiple records
      corresponding to that same Course record?  If not, then why are
      they separate tables?
      Yes, there can be multiple records in the Schedule table since the
      schedules are modified (in some cases) frequently up to the point that
      the course is actually offered. We need to be able to track all of
      the modifucations/schedule changes.

      Comment

      • Hugo Kornelis

        #4
        Re: Triggers

        On Fri, 14 Mar 2008 08:39:53 -0700 (PDT), t8ntboy wrote:
        >I have never used triggers before and was wondering what the easiest
        >way to perform the following scenario would be:
        >
        >I have a table called Courses and a table called Schedule. I would
        >like a corresponding record to be created automatcially in the
        >Schedule table when a record is added to the Course table.
        >
        >What is the best and easiest way to do this?
        Hi t8ntboy,

        CREATE TRIGGER MyTrigger
        ON dbo.Course AFTER INSERT
        AS
        IF @@ROWCOUNT = 0 RETURN;
        SET NOCOUNT ON;

        INSERT INTO Schedule (Col1, Col2, Col3, ...)
        SELECT Col1, Col2, Col3, ...
        FROM inserted;

        go

        Though you'd probably want to add some error handling.

        --
        Hugo Kornelis, SQL Server MVP
        My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

        Comment

        Working...