p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > SQL Server > SQL Server 2000 > SQL Server 2000
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 25th, 2005, 02:50 PM
Authorized User
Points: 418, Level: 7
Points: 418, Level: 7 Points: 418, Level: 7 Points: 418, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: Burbank, CA, USA.
Posts: 93
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

*******(*)*******
__________________
*******(*)*******
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old February 25th, 2005, 03:48 PM
Friend of Wrox
Points: 667, Level: 9
Points: 667, Level: 9 Points: 667, Level: 9 Points: 667, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: , , USA.
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old February 25th, 2005, 05:41 PM
Authorized User
Points: 418, Level: 7
Points: 418, Level: 7 Points: 418, Level: 7 Points: 418, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: Burbank, CA, USA.
Posts: 93
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?

*******(*)*******
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 09: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 07:54 AM



All times are GMT -4. The time now is 03:03 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc