Axelology
Posted At : September 27, 2007 8:59 AM | Posted By : Axel Jensen

SQL Update Tip

Just noting for myself on how to update tables from other tables...

Say you have a table called "customers" and you have a customerID as the primary key...

you now have a cross reference table that joins customers to certain products, it's only a 1 to 1 relationship in our situation (not very real world with this example for customers/products, but it's only an example), the database was made in flaw, so you want to do some clean up...

Your goal is to get rid of the cross reference table called "tblCustomerProduct" it has three columns, "customerID, productID, catID" you want to add the column "productID, and catID" to the "customers" table, and want to get rid of the cross reference table...

now you run your update, and it's a simple sql statement rather than looping over it in coldfusion the db runs it in no time!

--psuedo code
update c
SET c.productID = o.productID,
c.catID = o.catID
FROM customers
JOIN tblCustomerProduct o
ON c.customerID = o.customerID

works good...

again this is just psuedo code and is a reminder to myself to remember how to do this simple task, I would have done in coldfusion before, now i can use this ;)

Posted At : November 16, 2006 1:03 PM | Posted By : Axel Jensen

Using SQL sub select statments, or selects within select statments

In our team of developer's we use a tracking system, to track work orders, bug fixes, feature requests, and the status of those projects...

One of my recent assignments was to create a "Recently Updated List"

This list will contain the trackID, and a Title... it will sort by the date in descending order so we always see the most recent items first... It will join 2 tables - items(v_items), and events...

Items is the Main table, and Events are details about that item that happen along the tracking of that item...

[More]

Posted At : November 4, 2006 12:00 AM | Posted By : Axel Jensen

using sub selects with SQL UPDATE

Consider having the following tables. (Microsoft RMS)

-Item
-id
-cost

-SupplierList
-itemID
-supplierCost

when using RMS, You have an Item, it has it's own cost... maybe that cost is an average cost, or something like that, and that item may have multiple suppliers, and each supplier has an itemcost associated with it, this sucks with reporting and margins in RMS, because when you update the cost of the item, it doesnt update the supplier cost, because it doesnt know how to do so...

[More]




Blog provided and hosted by CF Webtools. Blog Sofware by Ray Camden.