Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 29th, 2003, 07:19 AM
Registered User
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using instead of trigger

Hi,

I'm trying to use a trigger to make sure no duplicates records would be added to my table (using sql server 2000). I've tried using the "instead of" trigger but for some reason it doen't work.

The code is :

Code:
CREATE TRIGGER tr_verifyDuplicates
ON tblAL
INSTEAD OF INSERT
AS
BEGIN  
  INSERT tblAL (column1,column2,column3)  
  SELECT column1,column2,column3   
  FROM INSERTED  
  WHERE NOT EXISTS  (
    SELECT * FROM tblAL     
    WHERE column1=(SELECT column1 FROM INSERTED)
    AND column2=(SELECT column2 FROM INSERTED)     
    AND column3=(SELECT column3 FROM INSERTED)
  )
END
I thought that the problem may be another column I have in the table which is an identity integer column (the table primary key-lets call it column0) but I don't know how to make it work.
The table has more than those 4 columns but all the other columns allow null values. Only the identity column and column2 which is a foreign key don't allow null.

Thanks for your help,
dmr

 
Old November 29th, 2003, 02:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

When you say it "doesn't work", what do you mean?

I don't know, but it might be easier and more straightforward to set up a unique constraint on the table, instead of using a trigger.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trigger fhillipo SQL Server 2000 1 March 20th, 2007 07:44 AM
Trigger Help monika.vasvani SQL Language 2 March 1st, 2007 06:59 AM
Trigger arshad mahmood C++ Programming 4 June 24th, 2004 07:10 AM
Trigger ! minhtri Pro VB Databases 2 June 23rd, 2004 02:27 AM
Trigger arshad mahmood SQL Language 2 May 12th, 2004 05:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.