Author |
Topic |
charlie82
Starting Member
5 Posts |
Posted - 2007-09-27 : 02:13:45
|
Hi All, I need help very badly and ASAP.I have one table name customer. I need to know how to write a trigger named AddOrder to ensure that the customer number entered foran order matches a customer number in the Customer table whenever a row in the Orders table is updated or inserted. If the customer name does not match, it will prompt a error message.Thanks,Charlie |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-27 : 02:23:05
|
[code]CREATE TRIGGER [AddOrder]ON dbo.[Orders]FOR INSERT, UPDATEASSET NOCOUNT ONIF EXISTS( SELECT * FROM inserted AS I LEFT OUTER JOIN [Customer] AS C ON I.[customer number] = C.[customer number] WHERE C.[customer number])BEGIN ROLLBACK RAISERROR ('Customer does not exist', 16, 1)ENDGO[/code]"order matches a customer number in the Customer table whenever a row in the Orders table is updated or inserted. If the customer name does not match"I presume you meant NUMBER for the second reference?Edit: Missed a bit!Kristen |
 |
|
charlie82
Starting Member
5 Posts |
Posted - 2007-09-27 : 02:41:46
|
Hi Kristen,Yes, Should be Customer number. Thanks!. Can i write the SQL as shown below? Is that correct?CREATE AddOrder TRIGGER ON ORDERFOR INSERT, UPDATEASIF NOT EXIST(SELECT * FROM CUSTOMER WHERE CustomerNum = new:CustomerNum ) /* Insert the new record into the Order table */ new:customerNumEntered = new:customerNum;ELSE/* Throw error that the customer number of the new order cannot be found in the customer table */RAISERROR ('Customer does not exist', 16, 1)END; |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-27 : 02:53:41
|
"Can i write the SQL as shown below? Is that correct?"Doesn't look right to me!What's the "new:" stuff doing?Also a trigger must be built to handle multiple rows being inserted/updated at once; you code looks to be working on t he basis that there is only one row.The "inserted" table in my example above holds the data for ALL the rows being inserted, or updated, and thus needs to be processed as a set, rather than as a single row.Kristen |
 |
|
charlie82
Starting Member
5 Posts |
Posted - 2007-09-27 : 02:57:45
|
Hi Kristen,The new is to insert a new record if the customer number matched. What i want is something like that.This trigger will activated when a new Order record is inserted or updated, and it will check if the CustomerNum of the new Order record exists in the Customer table. If it does, the new Order record will be inserted into the Order table. Else, an error will be thrown to the user that the customer number of the new order record cannot be found in the customer table. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 03:05:44
|
Seems to me that a plain ordinary foreign key contraint would fix this problem. E 12°55'05.25"N 56°04'39.16" |
 |
|
charlie82
Starting Member
5 Posts |
Posted - 2007-09-27 : 03:22:46
|
Hi,I need to write the SQL for /* Insert the new record into the Order table */ and /* Throw error that the customer number of the new order cannot be found in the customer table */ wih the example below. And it should come out with "This trigger will activated when a new Order record is inserted or updated, and it will check if the CustomerNum of the new Order record exists in the Customer table. If it does, the new Order record will be inserted into the Order table. Else, an error will be thrown to the user that the customer number of the new order record cannot be found in the customer table."CREATE AddOrder TRIGGER ON ORDERFOR INSERT, UPDATEASIF NOT EXIST(SELECT * FROM CUSTOMER WHERE CustomerNum = new:CustomerNum)/* Insert the new record into the Order table */ELSE/* Throw error that the customer number of the new order cannot be found in the customer table */END;Please help .... Thanks In advance .... |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-27 : 03:39:43
|
Triggers don't work like that, so if this is a home work assignment you'll have to find out what your teacher intended, because you certainly can't do "/* Insert the new record into the Order table */" with a standard "FOR" trigger.Next time please say that you are asking for help with homework because we don't do your work for you, although we will help with whatever you are stuck on.Kristen |
 |
|
charlie82
Starting Member
5 Posts |
Posted - 2007-09-27 : 03:52:28
|
Ops,Okok ... thanks thanks |
 |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2007-09-27 : 04:34:31
|
ahh, homework assignment.we need a homework forum.-ec |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-27 : 04:39:22
|
quote: Originally posted by eyechart ahh, homework assignment.we need a homework forum.-ec
What's the diff ? The general rules still apply. We don't do homework  KH[spoiler]Time is always against us[/spoiler] |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-27 : 04:42:15
|
or unless you are thinking of a forum where students post their homework assignment and other students or beginners can take an attempt at that ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2007-09-27 : 04:47:07
|
quote: Originally posted by khtan or unless you are thinking of a forum where students post their homework assignment and other students or beginners can take an attempt at that ? KH[spoiler]Time is always against us[/spoiler]
yeah, exactly. If we had a forum where we could help a bit instead of give answers outright. more of a learning forum than anything. this idea comes up a lot. And i think that it continually gets shot down. Anyway, I think it would take the guesswork out of when people are asking homework questions. -ec |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 04:49:25
|
A new forum named Homework? E 12°55'05.25"N 56°04'39.16" |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-27 : 04:56:09
|
Assignment / Homework / SchoolworkAnd also Interview Questions ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-27 : 05:33:00
|
Blimey, people are going to post in the right forum from now on? How cool is that ... |
 |
|
johnsql
Posting Yak Master
161 Posts |
Posted - 2007-09-27 : 12:26:14
|
quote: Originally posted by Peso Seems to me that a plain ordinary foreign key contraint would fix this problem. E 12°55'05.25"N 56°04'39.16"
The foreign/primary key relationship do not work if the 2 tables are designed in stand-alone way or people do not want to make such contraints when creating the tables, right? |
 |
|
johnsql
Posting Yak Master
161 Posts |
Posted - 2007-09-27 : 13:09:08
|
quote: Originally posted by Kristen
CREATE TRIGGER [AddOrder]ON dbo.[Orders]FOR INSERT, UPDATEASSET NOCOUNT ONIF EXISTS( SELECT * FROM inserted AS I LEFT OUTER JOIN [Customer] AS C ON I.[customer number] = C.[customer number] WHERE C.[customer number])BEGIN ROLLBACK RAISERROR ('Customer does not exist', 16, 1)ENDGO "order matches a customer number in the Customer table whenever a row in the Orders table is updated or inserted. If the customer name does not match"I presume you meant NUMBER for the second reference?Edit: Missed a bit!Kristen
---I have a question here. Right after the statement "SET NOCOUNT ON", why don't you If NOT Exists(...) instead of If Exists(...) ? |
 |
|
johnsql
Posting Yak Master
161 Posts |
Posted - 2007-09-27 : 13:10:08
|
quote: Originally posted by johnsql
quote: Originally posted by Kristen
CREATE TRIGGER [AddOrder]ON dbo.[Orders]FOR INSERT, UPDATEASSET NOCOUNT ONIF EXISTS( SELECT * FROM inserted AS I LEFT OUTER JOIN [Customer] AS C ON I.[customer number] = C.[customer number] WHERE C.[customer number])BEGIN ROLLBACK RAISERROR ('Customer does not exist', 16, 1)ENDGO "order matches a customer number in the Customer table whenever a row in the Orders table is updated or inserted. If the customer name does not match"I presume you meant NUMBER for the second reference?Edit: Missed a bit!Kristen
---I have a question here. Right after the statement "SET NOCOUNT ON", why don't you If NOT Exists(...) instead of If Exists(...) ?
Sorry my editting error. "Why don't you use ...?" |
 |
|
johnsql
Posting Yak Master
161 Posts |
Posted - 2007-09-27 : 13:12:17
|
quote: Originally posted by johnsql
quote: Originally posted by johnsql
quote: Originally posted by Kristen
CREATE TRIGGER [AddOrder]ON dbo.[Orders]FOR INSERT, UPDATEASSET NOCOUNT ONIF EXISTS( SELECT * FROM inserted AS I LEFT OUTER JOIN [Customer] AS C ON I.[customer number] = C.[customer number] WHERE C.[customer number])BEGIN ROLLBACK RAISERROR ('Customer does not exist', 16, 1)ENDGO "order matches a customer number in the Customer table whenever a row in the Orders table is updated or inserted. If the customer name does not match"I presume you meant NUMBER for the second reference?Edit: Missed a bit!Kristen
---I have a question here. Right after the statement "SET NOCOUNT ON", why don't you If NOT Exists(...) instead of If Exists(...) ?
Sorry my editting error. "Why don't you use ...?"
I think over here like this if I use IF NOT EXISTS(...), then I have to use INNER JOIN not LEFT JOIN, right?Thanks. |
 |
|
johnsql
Posting Yak Master
161 Posts |
Posted - 2007-09-27 : 13:16:53
|
quote: Originally posted by Kristen
CREATE TRIGGER [AddOrder]ON dbo.[Orders]FOR INSERT, UPDATEASSET NOCOUNT ONIF EXISTS( SELECT * FROM inserted AS I LEFT OUTER JOIN [Customer] AS C ON I.[customer number] = C.[customer number] WHERE C.[customer number])BEGIN ROLLBACK RAISERROR ('Customer does not exist', 16, 1)ENDGO "order matches a customer number in the Customer table whenever a row in the Orders table is updated or inserted. If the customer name does not match"I presume you meant NUMBER for the second reference?Edit: Missed a bit!Kristen
Well, I tried to use your IF EXISTS ( ... LEFT JOIN ...). The trigger fails even when I tried to insert a row into table Orders and that row's cutomerID column matches an entry in table Customer !So, I think we should use IF NOT EXISTS and an INNER JOIN for the trigger to work well. |
 |
|
Next Page
|