all groups > sql server programming > june 2007 >
You're in the

sql server programming

group:

Design question for inventory application


Design question for inventory application Edgard L. Riba
6/23/2007 9:55:38 AM
sql server programming:
Hi,
I have an inventory application where I keep track of items in different
warehouses. I have a table where I mantain the onhand inventory, the
current average cost, last cost, last purchase date, last date item was
sold, and a few other data.

Something like this:
CREATE TABLE iInv
(
warehouseNumber int NOT NULL,
itemID int NOT NULL,
QtyOnHand decimal(15),
avgCost decimal(10),
lastCost decimal(10),
CONSTRAINT PK_iInv PRIMARY KEY (warehouseNumber,itemID)
)

I then have transaction headers with their rows. The tables are called
iTrans and iTransRows.
iTransRows is something like:
CREATE TABLE iTransRows
(
siteID int NOT NULL,
transactionID int NOT NULL,
rowID int NOT NULL,
itemID int NOT NULL,
Qty decimal(15),
Cost decimal(10),
Price decimal(10),
CONSTRAINT PK_iTR PRIMARY KEY (siteID,transactionID,rowID)
)

In my application, once a transaction has been captured, it is "posted"
which means that the corresponding row in the table iInv (above) is updated
to reflect the current state in the inventory, taking into consideration the
type of inventory transaction entered.

I have been thinking lately that a BETTER design would be that the "posting"
process, INSTEAD of manipulating the iInv table directly, to generate some
sort of iInv_Transactions, and then with a trigger I update the iInv table.

I'm thinking something like:
CREATE TABLE iInv_Trans
(
iInvTransRowID int, -- auto generated
dt datetime,
itemID int NOT NULL,
transType int NOT NULL, -- To know effect on
inventory
Qty decimal(15),
Cost decimal(10),
Price decimal(10),
parentSiteID int,
parentTransID int,
parentRowID int,
CONSTRAINT PK_iInvTR PRIMARY KEY (iInvTransRowID)
)

The main reason I want to do this is that should there be any problem with
the table iInv, I would be able to rebuild it MUCH more easily with the
iInv_Transactions in place.

I would appreciate any comments on how is this typically done.

Best regards,
Edgard

Re: Design question for inventory application Roy Harvey
6/23/2007 1:11:23 PM
The main advantage to the approach you described is auditability. Add
a monthly snapshot of the current inventory, and it is easy to write a
routine that balances the transaction history since the snapshot with
the difference between the snapshot and the current numbers. Along
those same lines, when a physical inventory count results in an
adjustment, this way it will have to be recorded as a transaction for
the difference, and important thing to capture. That is far superior
to simply plugging in the physical count numbers. And I would add to
the transaction the identifier for the business transaction that
caused the action - shipment, receipt or adjustment - so that it can
all be tied back to the business.

Of course I don't know how much of an issue these sorts of things are
for your application, but it is easy to build it in now and horrible
to do it later. It isn't really more work to do it right if you start
early enough.

One thing I would do - I have done it the other way, and NEVER AGAIN -
is to make sure the transactions are stored so that a transaction that
adds to inventory is a positive quantity, while one that reduces
inventory is negative, and the same for dollars. It is SO MUCH easier
to simply SUM the numbers than to have to evaluate the transaction
type to set the sign before summing.

Roy Harvey
Beacon Falls, CT

On Sat, 23 Jun 2007 09:55:38 -0500, "Edgard L. Riba" <elriba at rimith
[quoted text, click to view]
Re: Design question for inventory application Edgard L. Riba
6/23/2007 5:13:08 PM
Hi Roy,
Thanks for responding.

[quoted text, click to view]

These are the sort of things that got me thinking about this. Right now, I
need to go through the transactions to reconstruct the "on hand inventory"
table, and that has proven cumbersome...

Do you use this type of design? Could you share with me how you designe
this table?

Thanks again,
Edgard

Re: Design question for inventory application Roy Harvey
6/23/2007 7:11:12 PM
The system I set up was back before relational databases, it was on
the mainframe and the database was IDMS. The ideas are the same
however.

There were two key tables. Inventory, keyed by warehouse and SKU,
holds the current inventory as well as some snapshot numbers mentioned
below. In that system it had buckets for available-to-ship and
on-hold (not available to ship). InventoryTransactions had one row
for every change to an audited column, which meant all quantity
columns. Other columns identified the status changed (on-hand or
held), the transaction type (customer shipment, transfer shipment,
receipt, adjustment, etc), and the shipment number or receipt number
or adjustment number of the business transaction making the change.
There were also buckets for the last physical inventory count, and (I
think) the date of that count.

Note that this was not a warehousing system with locator information
by individual unit. That would have required a good bit more
complexity.

If the system is built correctly there should not be any need to
reconstruct the on-hand inventory table. The exception would be if
you needed a point-in-time number that you had not saved as part of
the normal database design and system operation. When I did this we
captured the fiscal month-end inventory and the fiscal day-end
inventory. Since we only captured those two numbers they were kept on
the same inventory record that had the current on-hand. We never
neede any other point-in-time numbers.

There was a daily balance that double-checked the previous net change
against the day's transactions, and a similar test at month-end for
the full month's data. (The auditor who reviewed the system - there
was a lot more than inventory, it was a full order processing system
for a billion doller enterprise - said we had more controls and
cross-checks than he could keep track of.)

Today, with relational databases and triggers, I would be taking the
sort of path you described. The InventoryTransactions table would be
write-only, NO updates or deletions, and an INSERT trigger would do
all updates to the audited columns of the Inventory table. Inventory
would have triggers to block any other updates.

Roy Harvey
Beacon Falls, CT

On Sat, 23 Jun 2007 17:13:08 -0500, "Edgard L. Riba" <elriba at rimith
[quoted text, click to view]
AddThis Social Bookmark Button