| Author |
Topic |
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-13 : 12:43:45
|
Hi all,I want to create an INSTEAD OF INSERT trigger for following purpose but I cannot do it.Please help me.When a customer want to take an order then the trigger must check the customer's credit, if credit >= cost then insert a row to the order table else if credit <= cost then do not insert.I have two tables CREATE TABLE Customers( CustomerID nchar(10) PRIMARY KEY, Credit Int NOT NULL)CREATE TABLE Orders( OrderID nchar(10) PRIMARY KEY, CustomerID nchar(10), Cost int) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-13 : 13:08:16
|
| >>but I cannot do itWhat is preventing you from creating it?Does the credit need to be greater than (or equal to) just that one customer order or the sum of all the unpaid orders for that customer?Be One with the OptimizerTG |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-13 : 13:27:30
|
quote: Originally posted by TG >>but I cannot do itWhat is preventing you from creating it?Does the credit need to be greater than (or equal to) just that one customer order or the sum of all the unpaid orders for that customer?Be One with the OptimizerTG
I am a beginner.the credit need to be greater than just that customer order. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-13 : 13:31:09
|
Fair enough - so I guess this is for school, huh? Because it doesn't make sense to allow 1000 different orders all for 1 dollar less than their customer credit So as to Tara's question...Could you at least show us what you have so far?Be One with the OptimizerTG |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-13 : 13:56:07
|
Imagine I want to insert a row to the orders table with these values:OrderID='Order3', CustomerID='Customer1', Cost=120 DollorsThe Cost is greater than Costomer's Credit to this reason the row should not is inserted by trigger.OrderID CustomerID Cost---------- ---------- ---------------------Order1 Customer1 50.00Order2 Customer1 35.00 CustomerID Credit---------- ---------------------Customer1 100.00 |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-13 : 14:15:09
|
I think we understand the requirements - I believe what Tara was looking for was a little effort on your part before you got your homework handed to you on a platter. So I'll stand with here and wait your response But if those two rows each had 99.00 dollars you still don't mind adding new orders?Also, is it ok to all an UPDATE to change a cost to more than the credit?Be One with the OptimizerTG |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-13 : 15:34:25
|
Please just tell me how I must fill the ... at this code.CREATE TRIGGER trON OrdersINSTEAD OF INSERTAS... And please do not ask me more questions |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-13 : 15:45:35
|
Wow! That has got to be the lamest effort I've seen. Now I don't feel so bad just "handing" you this homework - you couldn't possibly misrepresent yourself in a job interview.set nocount onuse tempdbgoCREATE TABLE Customers( CustomerID nchar(10) PRIMARY KEY, Credit Int NOT NULL)goCREATE TABLE Orders( OrderID nchar(10) PRIMARY KEY, CustomerID nchar(10), Cost int)goinsert Customers values (N'00010', 10)gocreate trigger trg_i_orders on orders instead of insertasbegin insert Orders (OrderID, Customerid, Cost) select i.OrderID, i.CustomerID, i.Cost from inserted i inner join customers c on c.CustomerID = i.CustomerID where c.credit >= i.costendgoinsert Orders (OrderID, CustomerID, Cost) values (N'00012', N'00010', 8)insert Orders (OrderID, CustomerID, Cost) values (N'00011', N'00010', 12)insert Orders (OrderID, CustomerID, Cost) values (N'00013', N'00010', 8)insert Orders (OrderID, CustomerID, Cost) values (N'00014', N'00010', 8)select * from Ordersgodrop table ordersdrop table CustomersOUTPUT:OrderID CustomerID Cost---------- ---------- -----------00012 00010 800013 00010 800014 00010 8 Be One with the OptimizerTG |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-13 : 16:04:19
|
| Thank you so much.May you explain about this source "inserted".I have not written a trigger yet! |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-13 : 16:21:34
|
| You're welcome.read through this topic in Books Online. take a look at the samples and read about the virtual tables [inserted] and [deleted]Books Online - Create Trigger:http://technet.microsoft.com/en-us/library/ms189799(SQL.90).aspxEDIT:Then work through this topic and its children - great stuff - it's how I learned.Books Online - Query Fundamentals:http://technet.microsoft.com/en-us/library/ms190659(SQL.90).aspxBe One with the OptimizerTG |
 |
|
|
|