473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Can anyone spot the problem with my trigger?

In SQL Server 2005, I have an update trigger on a table. The purpose of this trigger is to set or clear a DeactivatedDate field whenever the IsActive field is changed. Also to write an audit record keeping a record of the change. Here is an excerpt from the trigger:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @prevIsActive BIT        
  2. DECLARE @newIsActive BIT
  3. DECLARE @id BIGINT
  4.  
  5. SELECT @newIsActive = IsActive, @id = RecordId
  6. FROM inserted
  7.  
  8. IF UPDATE(IsActive)    
  9. BEGIN    
  10.     SELECT @prevIsActive = IsActive FROM deleted
  11.     IF @newIsActive < @prevIsActive
  12.     BEGIN    --deactivated
  13.         UPDATE Customers 
  14.         SET DeactivatedDate = GETDATE() 
  15.         WHERE CustomerId=@id 
  16.     END
  17.     ELSE
  18.     BEGIN    --activated
  19.         UPDATE Customers 
  20.         SET DeactivatedDate = NULL 
  21.         WHERE CustomerId=@id 
  22.     END
  23.  
  24.     --Add an entry to the audit table with OpType 6 indicating deactivated and OpType 7 indicating activated.
  25.     INSERT INTO AuditCustomers (CustomerId, OpType, UserId)
  26.     SELECT i.CustomerId, CASE i.IsActive WHEN 1 THEN 7 ELSE 6 END, i.UserId
  27.     FROM Inserted AS i
  28.     INNER JOIN Deleted AS d ON i.CustomerId = d.CustomerId
  29.     WHERE i.IsActive <> d.IsActive
  30. END
  31.  
This trigger is working in 99% of cases. However, in the other 1% of cases, when a customer is deactivated, the audit record is created correctly with OpType=6, proving that the trigger must have fired, but the DeactivatedDate remains null. How is this possible, can anybody tell me?
Jan 14 '08 #1
0 878

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Harag | last post by:
Hi All Using: JScript IIS 5 I have a problem in the following code: // The next 4 lines display exactly what was typed in the text boxes. out("<br>Request.Form="+...
2
by: Stewart | last post by:
Hi, I'm hoping that someone will be able to help out with a problem we are having. We have a web service built in .Net 1.1. We are trying to call it from PHP4 using the NuSOAP add on. It...
3
by: TheBob | last post by:
I have read all related posts, but still cannot solve my issue. With my database and asp page in the root folder all works fine. <% set conn=Server.CreateObject("ADODB.Connection")...
3
by: JKop | last post by:
I have some code which when compiled on my system prints: <output> AnyClass Constructor for: blah AnyClass Copy Constructor for: Copy of blah AnyClass Copy Constructor for: Copy of Copy...
3
by: AndyG | last post by:
I'm trying to mess about with a table using javascript. I'm deleting the current row then adding a new row and adding a couple of columns. I then want to add either a style attribute and add a...
4
by: ii2o | last post by:
Hi guys, I want to set the value of a textbox when someone hovers over an image (which is hyperlinked) but to no avail. Can anyone spot the problem here? I've wittled the code down to it's bare...
2
by: Nicholas Sherlock | last post by:
Hey all, I wrote a class to represent a node in a site navigation tree which contains methods for building the tree from the database. I also wrote some routines to print out the whole tree,...
4
by: Tifer | last post by:
Hello, I'm still new to the whole .Net thing and I'm having a problem with something that should be so simple -- executing a query and returning an output parameter. It's a standard "Add"...
20
by: Pete Marsh | last post by:
Wondering if anyone can see an error with this script. I get a server configuration error. THat could mean a module is not being loaded, but maybe there's a syntax error here, can anyone spot it?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.