There was one thing I saw Paul and the reason we post DDL :). You can have
a NULL in these fields so therefore I changed the trigger around including
adding the changes for your table schema to compensate for this. Enjoy!
HTH,
Grant
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[TIU_Vendor_SetPreferredDiscontinuedFlag]') and
OBJECTPROPERTY(id, N'IsTrigger') = 1)
drop trigger [dbo].[TIU_Vendor_SetPreferredDiscontinuedFlag]
GO
CREATE TRIGGER TIU_Vendor_SetPreferredDiscontinuedFlag
ON dbo.tblVendor
FOR INSERT, UPDATE
AS
/*********************************HDR*************************************
*********GENERAL*********
OBJECT NAME: TIU_Vendor_SetPreferredDiscontinuedFlag
OBJECT TYPE: Trigger
SUBCATEGORY: Data Integrity
CREATED BY: GRANT CASE
DATE: 04/14/2004
MODIFIED BY: GRANT CASE
DATE: 04/14/2004
DESCRIPTION:
This trigger updates the tblVendor table and flips the flag v_prefer
and v_discon by the changes made within the application.
Example:
If v_prefer Flag is flipped to 1 then v_discon will be flipped to 0
If v_discon Flag is flipped to 1 then v_prefer will be flipped to 0
All new modifications of the row overwrite previous changes such that if a
row's v_prefer column were set to 1 and the new record sets the v_discon = 1
then the v_prefer column will have its value set back to 0.
*********VARIABLES*********
NAME TYPE LOCAL/PASSED DEFAULT
DESCRIPTION
---------------------------------------------------------------
*********DEPENDENCIES*********
TABLES:
OBJECTS:
*********TEMP TABLES*********
NAME DESCRIPTION/USE
*********TRIGGER SPECIFIC*********
SOURCE TABLE:
DESTINATION TABLE:
CASCADED TRG:
CASCADED TRG TABLE:
*********REPORT SPECIFIC*********
REPORTS USING STORED PROCEDURE:
*********OUTPUT*********
*********MODIFICATION LOG*********
DATE INITIALS MODIFICATION
04/14/2004 GSC Created
*********HEADER CONVENTIONS*********
DO NOT GO PAST 75 CHARACTERS BEFORE GOING TO THE NEXT LINE
TO DEBUG, REPLACE ALL "-- DEBUG"
*********************************HDR*************************************/
--------------------------------------------------------------------------
-- LOCAL VARIABLE DECLARATION SECTION
--------------------------------------------------------------------------
DECLARE @ActionType AS VARCHAR (10)
--------------------------------------------------------------------------
-- LOCAL VARIABLE SET SECTION
--------------------------------------------------------------------------
--IF YOU DO NOT SET THIS, ACTIVE SERVER PAGES WILL HAVE PROBLEMS
SET NOCOUNT ON
SET @ActionType = CASE
WHEN NOT EXISTS (SELECT 1 FROM DELETED) AND NOT EXISTS (SELECT 1 FROM
INSERTED) THEN 'None'
WHEN EXISTS (SELECT 1 FROM DELETED) AND EXISTS (SELECT 1 FROM INSERTED)
THEN 'Update'
WHEN NOT EXISTS (SELECT 1 FROM DELETED) AND EXISTS (SELECT 1 FROM INSERTED)
THEN 'Insert'
WHEN EXISTS (SELECT 1 FROM DELETED) AND NOT EXISTS (SELECT 1 FROM INSERTED)
THEN 'Delete'
END
--------------------------------------------------------------------------
-- TEMP TABLE CREATION SECTION
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- CODE SECTION
--------------------------------------------------------------------------
IF @ActionType = 'Update'
BEGIN
/*************************************************************************
Test to ensure that the records being modified are not both being flipped
to ON for the first time. If a record is already flipped to 1 and the
other flag is flipped to 1 then the previously flipped flag will be
flipped to 0.
*************************************************************************/
IF EXISTS (SELECT 1 FROM Inserted
INNER JOIN Deleted ON Inserted.vend_id = Deleted.vend_id
WHERE ((Inserted.v_discon <> Deleted.v_discon) OR (Inserted.v_discon = 1
AND Deleted.v_discon IS NULL))
AND ((Inserted.v_prefer <> Deleted.v_prefer) OR (Inserted.v_prefer = 1 AND
Deleted.v_prefer IS NULL))
AND Inserted.v_prefer = 1 AND Inserted.v_discon = 1)
BEGIN
RAISERROR ('v_discon and v_prefer Flag may not be set to true at the same
time',16, 1)
ROLLBACK TRANSACTION
RETURN
END
UPDATE tblVendor
SET tblVendor.v_prefer = CASE
WHEN (Deleted.v_discon IS NULL OR Deleted.v_discon = 0) AND
Inserted.v_discon = 1 THEN 0
ELSE tblVendor.v_prefer
END,
tblVendor.v_discon = CASE
WHEN (Deleted.v_prefer IS NULL OR Deleted.v_prefer = 0) AND
Inserted.v_prefer = 1 THEN 0
ELSE tblVendor.v_discon
END
FROM tblVendor
INNER JOIN Inserted ON tblVendor.vend_id = Inserted.vend_id
INNER JOIN Deleted ON Inserted.vend_id = Deleted.vend_id
WHERE ((Deleted.v_discon = 0 OR Deleted.v_discon IS NULL) AND
Inserted.v_discon = 1)
OR ((Deleted.v_prefer = 0 OR Deleted.v_prefer IS NULL) AND
Inserted.v_prefer = 1 )
IF @@ERROR <> 0 ROLLBACK TRANSACTION
END
IF @ActionType = 'Insert'
BEGIN
/*************************************************************************
Test to ensure that the records being entered do not have both a v_prefer
and v_discon flag set to 1.
*************************************************************************/
IF EXISTS (SELECT 1 FROM Inserted WHERE Inserted.v_prefer = 1 AND
Inserted.v_discon = 1)
BEGIN
RAISERROR ('v_discon and v_prefer Flag may not be set to true at the same
time',16, 1)
ROLLBACK TRANSACTION
RETURN
END
UPDATE tblVendor
SET tblVendor.v_prefer = CASE
WHEN Inserted.v_discon = 1 THEN 0
ELSE tblVendor.v_prefer
END,
tblVendor.v_discon = CASE
WHEN Inserted.v_prefer = 1 THEN 0
ELSE tblVendor.v_discon
END
FROM tblVendor
INNER JOIN Inserted ON tblVendor.vend_id = Inserted.vend_id
IF @@ERROR <> 0 ROLLBACK TRANSACTION
END
--------------------------------------------------------------------------
-- TESTING SECTION
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- CLEAN UP SECTION
--------------------------------------------------------------------------
GO
[quoted text, click to view] "PaulJS" <anonymous@discussions.microsoft.com> wrote in message
news:F8F51E6A-718B-46C0-A287-87170B859FEE@microsoft.com...
> Grant:
>
> I'm amazed at the detail you've provided! I'm not sure it's what your
looking for, but below is the text from a SQL script file that defines the
table in question (tblVendor).
[quoted text, click to view] >
> Notes: The front end of this system is a Microsoft Access XP database (MDB
format, not an Access Project). Until Jan. 1 of this year, the back end was
also Access, but our company took over a competitor across the country and
we had to immediately upsize to SQL Server. We've been going thru the