Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Pilbum
Starting Member
1 Post |
Posted - 2012-07-12 : 08:52:05
|
Hello,I'm trying to create a trigger that will clear the "checked" field when all items with the same classification have been checked.The purpose of this is to do cycle counting of inventory, which is when you count the stock levels of a proportion of stock that have the same classification.The checked field will be used to record the date it was lasted checked and will cleared once all stock have counted, putting back into the pool of stock to be counted.Therefore, the trigger is suppose to count the number of rows that share the same classification as the amended row, and compare that with the number of rows in that classification, that have a date in the checked date field. When the two values are the same, reset all the checked dates to blank.But when I tested it, nothing happened :( Does anyone know why?ALTER TRIGGER [dbo].[UDEF_Last_Checked_Reset] ON [dbo].[ASC_PMA_TBL]AFTER INSERTASBEGINDECLARE @PartOnly varchar(16) -- Part NumberDECLARE @Rev varchar(4) -- Part RevisionDECLARE @PartCode as varchar(2) -- Part ClassificationDECLARE @Last_Checked as datetime -- Checked DateSELECT @PartOnly = PMA_PART_ONLY, @Rev = PMA_PART_REV, @PartCode = PMA_PART_CODE, @Last_Checked = [PMA_LAST_CHECK_DATE]FROM Inserted-----------------------------------------------------------------------------declare @countpartcode as decimal(18,5) --Counter for part classificationdeclare @countlastcheck as decimal(18,5) -- Counter for completed parts--set countersset @countpartcode = (SELECT SUM(case when [PMA_PART_CODE]=@PartCode THEN 1 ELSE 0 END)from ASC_PMA_TBL)set @countlastcheck = (select SUM(case when [PMA_LAST_CHECK_DATE] IS NULL AND PMA_PART_CODE = @PartCode THEN 1 ELSE 0 END) from asc_pma_tbl)--compare countersif @countpartcode = @countlastcheck -- clear checked date field on all parts with same classifcation UPDATE [ASC_PMA_TBL]SET [PMA_LAST_CHECK_DATE] = ''WHERE [PMA_PART_CODE] = @PartCode END |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-12 : 10:04:10
|
your trigger is assuming only one row change at a time. Please keep in mind that for batch inserts trigger will be called once where inserted will have entire inserted records. So you might want to use table variable to get values from inserted table rather than variables.Second thing is reg. your requirement. can you elaborate on how and when stock checking works? Does this have to be triggered by an insert?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2012-07-12 : 10:12:39
|
I already prepared this by the time Visakh responded but I'll post anyway...couple problems:1. you are setting check column to an empty space but checking for NULL.2. you are only handling inserts - not updates to the table3. you are only dealing with one row - what if more than one row is inserted or updated?Perhaps this:ALTER TRIGGER [dbo].[UDEF_Last_Checked_Reset] ON [dbo].[ASC_PMA_TBL]AFTER INSERT, updateASBEGIN update a set a.[PMA_LAST_CHECK_DATE] = NULL From ASC_PMA_TBL a join ( select i.pma_part_code from inserted i join ASC_PMA_TBL a on a.pma_part_code = i.pma_part_code group by i.pma_part_code having count(*) = sum(case when isNull([PMA_LAST_CHECK_DATE],'') = '' then 1 else 0 end) ) d on d.ASC_PMA_TBL = a.ASC_PMA_TBLendgo Be One with the OptimizerTG |
 |
|
|
|
|
|
|