Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with triggers

Author  Topic 

bigkampe
Starting Member

3 Posts

Posted - 2010-12-21 : 15:58:58
Ok, I am having difficulty understanding the syntax of a trigger.
I have 2 tables.
First table is River_Assessments which has 2 columns: ID and Listing_Category.
Second table is River_Segments which aslo has 2 columns: ComID, and Max_Pollutant.

The basic rule is Listing_Category MUST BE THE SAME AS Max_Pollutant.

Here’s what I want the trigger to do:
Anytime a Listing_Category value is changed in River_Assessments table, the value needs to be changed in the Max_Pollutant field of the River_Segments WHERE ID = ComID.

Can someone help please? I am a GIS Analyst by trade who has been thrust into the challenging and often confusing world of databases.

Thanks in advance.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-12-21 : 16:04:44
Do you ahve some sample data and expected results? Is there a one to one relationship between the two tables or are there many Rivver_assessments and you want to take the MAX listing_category for a given ID and use that max to update the River_segments table? Depending on the relationship between the tables there might be other options besides a trigger.

This link might help preparing data:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

bigkampe
Starting Member

3 Posts

Posted - 2010-12-21 : 16:31:55
quote:
Originally posted by Lamprey

Do you ahve some sample data and expected results? Is there a one to one relationship between the two tables or are there many Rivver_assessments and you want to take the MAX listing_category for a given ID and use that max to update the River_segments table? Depending on the relationship between the tables there might be other options besides a trigger.

This link might help preparing data:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx




These are excellent questions. While there is more going on than just these two tables, I want a simple example of a trigger executing a change in one table based on a user-change from another table. So, let's assume a one to one relationship between ID and ComID. And, I want the simplest behavior possible. When someone modifies a Listing_Category value (Example Listing_Category goes from 2 to 4a), the same value is changed in Max_Pollutant (2 to 4a). Basically, I do not want editors having to edit multiple tables.

I hope this clears it up. These tables are actually huge and I am trying to keep the River_Segments table up to date with the pollutant info for a mapping application to be used for the EPA. Normally, I'd create a view that sorts through all the data, but I'm intrigued by the use of triggers.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-12-21 : 17:29:39
I'm not convinced that trigger are the correct solution or that the problem shouldn't be solved another way (like a a view you mentioned). But, if you are doing some leaning, here is a update trigger sample:
-- Setup
CREATE TABLE dbo.River_Assessments (ID INT, Listing_Category INT)
CREATE TABLE dbo.River_Segments (ComID INT, Max_Pollutant INT)

INSERT dbo.River_Assessments
(ID , Listing_Category)
VALUES
(1, 1),
(2, 1),
(3, 1),
(4, 1),
(5, 1)

INSERT dbo.River_Segments
(ComID , Max_Pollutant)
VALUES
(1, 1),
(2, 1),
(3, 1),
(4, 1),
(5, 1)
GO

-- Create Trigger
CREATE TRIGGER dbo.River_Assessments_Update
ON dbo.River_Assessments
AFTER UPDATE
AS
BEGIN
MERGE
dbo.River_Segments AS Target
USING
Inserted AS Source
ON Source.ID = Target.ComID
WHEN MATCHED THEN
UPDATE SET Target.Max_Pollutant = Source.Listing_Category;

END

-- Check starting point
SELECT * FROM dbo.River_Assessments
SELECT * FROM dbo.River_Segments

--Change data
UPDATE dbo.River_Assessments
SET Listing_Category = 5
WHERE ID IN (2, 4)

-- Verify results
SELECT * FROM dbo.River_Assessments
SELECT * FROM dbo.River_Segments

-- Cleanup
DROP TRIGGER dbo.River_Assessments_Update
DROP TABLE dbo.River_Assessments
DROP TABLE dbo.River_Segments
Go to Top of Page

bigkampe
Starting Member

3 Posts

Posted - 2010-12-21 : 17:51:35
This is great Lamprey! I really appreciate your help. I'll give this a whirl tomorrow.
Go to Top of Page
   

- Advertisement -