|
|
 |
| 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.
|
 |

February 25th, 2005, 02:50 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Location: Burbank, CA, USA.
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
*******(*)*******
__________________
*******(*)*******
|

February 25th, 2005, 03:48 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

February 25th, 2005, 05:41 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Location: Burbank, CA, USA.
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
*******(*)*******
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |