Re: Triggers
- From: Hugo Kornelis <hugo@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 18 Mar 2008 21:47:04 +0100
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
.
- References:
- Triggers
- From: t8ntboy
- Triggers
- Prev by Date: Trigger to modify a table
- Next by Date: what occurs in a comparison when a value is NULL
- Previous by thread: Re: Triggers
- Next by thread: Re: Question: performance of MIN vs. Group By
- Index(es):
Relevant Pages
|