473,395 Members | 1,629 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,395 software developers and data experts.

SQL Update Trigger Question

Hello all,

I am trying to implement a trigger on a Microsoft SQL Server 2000 database. I have created the trigger, and it works mostly like it should, however I have a slight problem I can't seem to get around.

We use two fields for this trigger. Both fields are 3 digit numbers (varchar(3) in the database, I have no control over that). The second field is used to store the value of the first field only if the first field has changed, and only the first time this is done. By default, both fields are blank. The second field is read-only.

For example, if I put the number 555 into the first field, I want that number populated in the second field, so it reads 555. If I then change the first field to 444, I want the second field to remain 555, and so on, no matter how many times field 1 is changed.

My trigger is able to update the second field, but I can't figure out the logic needed to store the value of field 1 in field 2 only the first time field 1 is updated.

The trigger is below. Field 1 is identified as C536871310, Field 2 is C536870924:

USE [RSystem]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[MRUpdate]

ON [dbo].[T311]

FOR update

AS

Declare @MR varchar(3)

Declare @OrigMR varchar(3)

Declare @Ticket varchar(15)



If Update(C536871310)

Begin

Select @MR = (Select C536871310 from deleted)

Select @OrigMR = (Select C536871310 from inserted)

Select @Ticket = (Select C1 from inserted)

if @MR = NULL
update T311 set T311.C536870924 = @OrigMR WHERE C1 = @Ticket
else
update T311 set T311.C536870924 = @MR WHERE C1 = @Ticket
End
GO
Aug 26 '08 #1
2 1374
ck9663
2,878 Expert 2GB
Although your trigger can be optimized, by doing a single UPDATE statement, I won't change it. So far you did good.

The problem is in this part

"if @MR = NULL"

You can not use equal sign to check for null. Use IS NULL or IS NOT NULL instead.

Read more here


-- CK
Aug 26 '08 #2
Thank you, that seemed to fix the issue.

I wonder why the parser didn't catch that? I was having a non sql moment apparently.
Aug 28 '08 #3

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

Similar topics

2
by: imani_technology | last post by:
How would I write a trigger that updates the values of a Description column to upper case for even IDs and to lower case for odd IDs? I need this trigger to fire for INSERT and UPDATE events.
8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
1
by: Gent | last post by:
am using FOR UPDATE triggers to audit a table that has 67 fields. My problem is that this slows down the system significantly. I have narrowed down the problem to the size (Lines of code) that need...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
2
by: Josué Maldonado | last post by:
Hello list, Is there a way in pg to fire a function when a complete (not row by row) set is updated/inserted/deleted, for instance. update order_detail set confirmed='S' where...
3
by: V T | last post by:
Hello all, SQL Server 2000 documentation http://www.microsoft.com/technet/prodtechnol/sql/2000/reskit/part10/c3761.mspx states that if view is using "NOT NULL" columns of a base table, then...
5
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
1
by: Ryandmcbee | last post by:
I am trying to write an update trigger that will send one field from one table (header) to update one field in another table (multiple rows though). Here is what I have so far, the syntax is...
13
by: Neil | last post by:
I'm running an update query in SQL 7 from QA, and it runs forever. Has been running for 20 minutes so far! The query is quite simple: update a single field in a table, based on a join with another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.