Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Newbie trigger question
I will apologize in advanced for spamming the group with newbie
questions. I do not have a reference in front of me and several google
attempts have come up on sucessful for the kind of information that I
am looking for. That being said here is my question/problem:

In a MSSQL database I want to create a trigger. I have 'product'
table where products can be linked together via a 'link' table to other
products. In this particular situation there can be a kind of master
product that other products are then cloned(i.e. they have the same
record values other than primary key) from (yes lot of data
redundancy!). If a clone is made from the master there is a  link
between the two id's of the 'product' table made in the 'link' table.
Now the trigger must coordinate the the master products prices with
the linked  products.

Here is a query that does what I want I just need it in trigger form.

update product
set price = (select price from product where prod_id = xxxyz)
where prod_id in (select related_id
from link
where id = xxxyz)

where xxxyz is the prod_id of the master product whose price is being
updated.

(I know this is not the ideal database set up but it is what I have to
work with )

- thanks!
Jake


Report this thread to moderator Post Follow-up to this message
Old Post
alacrite@gmail.com
09-29-05 01:24 AM


Re: Newbie trigger question
-- Does the following Query do what your query is supposed to
-- If so the trigger should be about what you want.

UPDATE dest SET dest.price = src.price
-- SELECT dest.price, src.price, dest.prod_id, src.prod_id
FROM product src
JOIN link l ON src.prod_id = l.id
JOIN product dest ON l.related_id = dest.prod_id
WHERE dest.prod_id <> src.prod_id AND src.prod_id = xxxyz

CREATE TRIGGER product__update ON dbo.product
FOR UPDATE AS
BEGIN

UPDATE dest SET dest.price = src.price
FROM inserted src
JOIN link l ON src.prod_id = l.id
JOIN product dest ON l.related_id = dest.prod_id
WHERE dest.prod_id <> src.prod_id
-- AND dest.price <> src.price -- Add this if Price is a NOT NULL
Column
END

Tim S


Report this thread to moderator Post Follow-up to this message
Old Post
Tim
09-29-05 01:24 AM


Re: Newbie trigger question
Since I guess you are going to do multiple columns you should not use
"AND dest.price <> src.price"

Since it would only update if price is different.

Tim S


Report this thread to moderator Post Follow-up to this message
Old Post
Tim
09-29-05 01:24 AM


Re: Newbie trigger question
Please do not multi-post!

I answered in microsoft.public.sqlserver.programming

--
David Portas
SQL Server MVP
--


<alacrite@gmail.com> wrote in message
news:1127934231.076210.62130@g47g2000cwa.googlegroups.com...
>  I will apologize in advanced for spamming the group with newbie
> questions. I do not have a reference in front of me and several google
> attempts have come up on sucessful for the kind of information that I
> am looking for. That being said here is my question/problem:
>
>  In a MSSQL database I want to create a trigger. I have 'product'
> table where products can be linked together via a 'link' table to other
> products. In this particular situation there can be a kind of master
> product that other products are then cloned(i.e. they have the same
> record values other than primary key) from (yes lot of data
> redundancy!). If a clone is made from the master there is a  link
> between the two id's of the 'product' table made in the 'link' table.
>  Now the trigger must coordinate the the master products prices with
> the linked  products.
>
> Here is a query that does what I want I just need it in trigger form.
>
> update product
> set price = (select price from product where prod_id = xxxyz)
> where prod_id in (select related_id
> from link
> where id = xxxyz)
>
> where xxxyz is the prod_id of the master product whose price is being
> updated.
>
> (I know this is not the ideal database set up but it is what I have to
> work with )
>
> - thanks!
> Jake
>



Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
09-29-05 01:24 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 02:05 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006