| Author |
Topic |
|
s2002
Starting Member
49 Posts |
Posted - 2007-09-25 : 18:17:04
|
| hi,I have 2 tables.Products (ProductID int PK,ProductName Nvarchar,CatID int FK)Category (CatID int pk, CatName Nvarchar, TotalProducts).I need to create a trigger, when OnInsert in Products table, thenbase on Product Category -CatID-, "TotalProducts" field in Category table Would Increament 1.What should be the query Structure.Thanks, |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 18:30:09
|
| Could you show us how far you got as your problem appears to be a homework question? We don't mind helping people out with their homework, but we would like to see that the poster at least triedTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
s2002
Starting Member
49 Posts |
Posted - 2007-09-25 : 19:02:52
|
| Thanks God for generosity of Peoples Like You, Who Do my HomeWorks.Any way, My Code is belowDECLARE @ProductID AS intSET @ProductID = (SELECT TOP 1 [Id] FROM Inserted ORDER BY [Id]) WHILE @ProductID IS NOT NULL BEGIN DECLARE @CategoryId int; SELECT @CategoryId = CategoryId FROM Inserted WHERE ProductID = @ProductID; BEGIN UPDATE Category SET TotalProducts = TotalProducts + 1WHERE CatId IN SELECT CatId FROM Products WHERE ProductID=@ProductID END |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 19:27:54
|
| Could you provide a data example of what you need so that your problem is more clear?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
s2002
Starting Member
49 Posts |
Posted - 2007-09-25 : 21:47:56
|
| Thansk for your Reply tkizer,CAtID CatName TotalProducts1 Beverages 122 Condiments 123 Confections 13.....ProductID ProductName CatID1 Chai 12 Chang 13 Aniseed Syrup 2.....What I want trigger to do isIf I insert a new Product Record into Products Table -insert into products values ("aaaa","1")-Then since CatID=1 then "TotalProducts" Value for CAtID=1 should beUpdate to 13 (12+1)and if CatID was 3 then the TotalProducts" Value for CAtID=1 should be Update to 14(13+1) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-26 : 12:43:38
|
| This should do it. You shouldn't need any variables or other statements as a simple join to the inserted rows gives you all that you need.UPDATE cSET TotalProducts = TotalProducts + 1FROM Category cINNER JOIN inserted iON c.CatID = i.CatIDYou'll need to wrap it into a trigger.I would think that your assignment would be to insert the row into Category table if a new category gets inserted into Products table, but perhaps the instructor only cares about the UPDATE statement.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-26 : 13:07:18
|
| Well you have to read my whole sentence and not just quote the first part. Most homework questions that show the person is really trying get answers from us. The posts that just copy the instructor's problems verbatim here do not get answered.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-26 : 14:55:26
|
"the instructor's problems"I agree with that. I have a problem with most of the Students homework in terms of the ineptitude & inexactitude of the Professor/Teacher setting the question   Kristen |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-26 : 15:08:02
|
| LMAOTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
|