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 February 25th, 2005, 02:50 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default Insert Trigger

Hello,

I am trying to write a trigger that will not allow an insert to take place if specific column entry already exists. The following code is what I've come up with. How can I make it better? Any suggestions?

CREATE TRIGGER trForInsert ON [dbo].[ProjectInfo]
FOR INSERT
AS
declare @EmpNo int
declare @ExistingEmpNo int
set @EmpNo = (select EmpNo from Inserted)
set @ExistingEmpNo = (select EmpNo from ProjectInfo)

if(@EmpNo = @ExistingEmpNo )
begin
Rollback transaction
end

Thanks,
Arsi

*******(*)*******
__________________
*******(*)*******
 
Old February 25th, 2005, 03:48 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Do you really have to use trigger? Not sure about your design but can you use a Unique Constraint? or if you are using stored procedure to insert then something like this

Create Procedure AddInfo
@Empno int
AS
Begin
IF NOT EXISTS (SELECT EmpNo FROM ProjectInfo WHERE EmpNo=@EmpNo)
INSERT INTO ProjectInfo VALUES (@EmpNo)
End
 
Old February 25th, 2005, 05:41 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

Hello Thanks for your help I appreciate it!

I would like to use a trigger, but I don't have to. what you showed me helped a little, but i can't complete it. What am I doing wrong?
CREATE TRIGGER [CheckNum] ON [dbo].[Table]
FOR INSERT, UPDATE
AS
declare @Ac nchar(10)
IF UPDATE(Acronym)
IF EXISTS (SELECT Acronym FROM INSERTED WHERE Acronym = @Ac)
BEGIN
ROLLBACK TRANSACTION
END

The above works if I say WHERE Acronym = 'tst' or any other specific data contained in that column.

Also, is there a reason why it would work on one db and not on another?

*******(*)*******





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Trigger debbiecoates SQL Server 2000 3 December 24th, 2007 08:00 AM
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Create Insert Trigger byron SQL Server 2000 6 November 19th, 2003 11:30 AM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM





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