Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2003, 12:09 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default Updating unitsOnOrder

Can some one help with this situation. I am using the sample northwind Database with and inventory control data tables, purchase order and inventory transaction. I need to update the unitsOnOrder in the products table when insert occurs in the inventory transaction table.
I created this trigger:

CREATE TRIGGER "bcc.Trigger_UpdateProducts" ON InventoryTransactions
FOR INSERT
Not for replication
AS
UPDATE
dbo.products
SET
UnitsOnOrder=dbo.InventoryTransactions.UnitsOrdere d
FROM
inserted,dbo.InventoryTransactions
where dbo.products.productID=dbo.InventoryTransactions.p roductID

but I get this error "key column information is insufficient or incorrect. Too many rows were affect by the update"

Can anyone help me correct the trigger to avoid the error.
Pleas make suggestion or recommendation on any part of this request.


__________________
Jaime E. Maccou
 
Old November 28th, 2003, 04:05 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

keep looking on how you are using the inserted table
you must precede every column with the table have if two tables have the same column name


Ahmed Ali
Software Developer
 
Old November 28th, 2003, 08:06 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

I may not be clear on what you are saying, Please correct if I amd wrong.

CREATE TRIGGER "bcc.Trigger_UpdateProducts" ON InventoryTransactions
FOR INSERT
Not for replication
AS
UPDATE
dbo.products
SET
dbo.products.UnitsOnOrder=dbo.InventoryTransaction s.UnitsOrdered
FROM
dbo.InventoryTransactions, inserted
where dbo.products.productID=dbo.InventoryTransactions.p roductID


 
Old November 28th, 2003, 02:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:CREATE TRIGGER "bcc.Trigger_UpdateProducts" ON InventoryTransactions
FOR INSERT
Not for replication
AS
UPDATE
dbo.products
SET
dbo.products.UnitsOnOrder=dbo.InventoryTransaction s.UnitsOrdered
FROM
dbo.InventoryTransactions, inserted
where dbo.products.productID=dbo.InventoryTransactions.p roductID
Please look your code again, as I see it there, You have used the inserted table only in the FROM clause. But you dont use the same in the WHERE clause, without which the query is meaningless. Going by the code you posted, it would update all the records in PRODUCT table, every time an INSERT event occurs on InventoryTransactions Table. Thats why you get too many rows updated as you said.

If I am not wrong, it should be like this. Every time a record is inserted into InventoryTransactions, the same records ProductId is used as a look up value in Products table and its UnitsOnOrder should get updated. If that is so, then your Update statement should be as follows.

CREATE TRIGGER "bcc.Trigger_UpdateProducts" ON InventoryTransactions
FOR INSERT
Not for replication
AS
UPDATE dbo.products SET
dbo.products.UnitsOnOrder=Inserted.UnitsOrdered
FROM dbo.InventoryTransactions, inserted
where dbo.products.productID=Inserted.productID

Coz, Inserted table is a replica of the record that you are trying to insert.

Also, I noticed another logical error. Everytime the Record is inserted, should the PRODUCTS.UnitsOrdered be incremented? If so, it should look like

UPDATE dbo.products SET
dbo.products.UnitsOnOrder=dbo.products.UnitsOnOrde r + Inserted.UnitsOrdered
FROM dbo.InventoryTransactions, inserted
where dbo.products.productID=Inserted.productID

Hope that helps.

Cheers,

-Vijay G
 
Old November 28th, 2003, 03:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

I forgot to remove the "dbo.InventoryTransactions" from the FROM clause in both the occasion in my previous post, as it is no more required there, since we dont use that table for any kidn of reference. Please do so at you end and let me know if that works.

Cheers,

-Vijay G
 
Old November 28th, 2003, 07:09 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Thank you so much for opening my eyes. IT worked.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a datagrid jgrant ASP.NET 2.0 Basics 0 August 25th, 2007 07:34 PM
Updating Recordset arholly Access VBA 16 December 27th, 2006 01:28 PM
database not updating ellie BOOK: Beginning ASP 3.0 1 April 16th, 2006 05:28 AM
updating many to many whyulil Classic ASP Databases 4 March 11th, 2004 12:58 PM
Updating dmurray Access 1 June 25th, 2003 05:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.