Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Trigger to check for duplicates

Author  Topic 

QueenKirsty
Starting Member

18 Posts

Posted - 2009-05-13 : 04:36:34
Please can someone provide me with a solution to the following:

I have a table (Equipment) that holds details about items of equipment. The interesting columns of this table are:
EquipmentID (PK - integer)
MSerialNumber (nvarchar - allows Nulls).

I would like to have a trigger on the table so that when a record is added to the table or when the field MSerialNumber is updated it checks to see if that serial number already exists in the table. If it does I would like the update / insert to be cancelled and an error message displayed.

I have an additional issue that I would like to ignore all punctuation marks, spaces and preceding zeros in the MSerialNumber field when comparing them.
E.G.s
000000N124F6 matches N124F6
lp-240 matches lp240
00LPZ-R560 matches LPZR560

Commonly, the punctuation is only either spaces or hyphens.

All assistance gratefully appreciated.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-13 : 04:47:32
you could write a function to remove leading zeroes, dashes, spaces etc before inserting the MSerialNumber into the table.

And in your insert statement just validate the input SerialNumber (after removing leading zeroes etc) against the table.

insert into Equipment (EquipmentID, MSerialNumber)
select @EquipmentID, @MSerialNumber
where not exists (select * from Equipment x where x.EquipmentID = @EquipmentID)
and not exists (select * from Equipment x where x.MSerialNumber = FN_CLEANUP_SERIALNO(@MSerialNumber))



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

QueenKirsty
Starting Member

18 Posts

Posted - 2009-05-13 : 04:54:57
Thanks. That looks like the right start but how do I make it into a trigger on the table? I want it on the table because there are a lots of places in my interface where you can insert / add / edit records and these do not call a SP to do so.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-13 : 04:57:47
see here
http://www.sqlteam.com/article/an-introduction-to-triggers-part-i

you still have to create the function to perform the necessary clean up to remove any unwanted chars from the serial no


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

QueenKirsty
Starting Member

18 Posts

Posted - 2009-05-14 : 11:45:07
I have now solved the problem with a function to strip the serial number and a trigger to do the check. I have attached the code for anyones interest in the future. Thanks for all assistance give!

FUNCTION


Create function dbo.StripSerialNo (@MSerialNumber nvarchar(200))
RETURNS nvarchar(200)
as
begin
declare @strNoHyphens varchar(200),
@strNoSpaces varchar(200),
@strNoZeros varchar(200)

set @strNoHyphens = Replace(@MSerialNumber, '-', '')
set @strNoSpaces = replace(@strNoHyphens, ' ', '')
set @strNoZeros = replace(LTRIM(replace(@strNoSpaces, '0', ' ')), ' ', '0')
return(@strNoZeros)
end



TRIGGER

CREATE trigger check_Serial_No 
on dbo.Equipment
for Insert, update

as
declare @SNstripped nvarchar(200),
@SN nvarchar(200),
@MID integer,
@SerialNo nvarchar(200),
@EID integer

select @SN = MSerialNumber, @MID = ManufacturerID, @EID = EquipmentID
from inserted

if @SN is not null
Begin
set @SNstripped = dbo.StripSerialNo(@SN)
--if serial number exists already then
if exists (select MSerialNumber, ManufacturerID
from dbo.Equipment
where (dbo.StripSerialNo(MSerialNumber) = @SNstripped) and (ManufacturerID = @MID) and (EquipmentID <> @EID))
begin
raiserror ('An Equipment record already exists with that serial number.
The record will nno be saved at this time.',16,1)
rollback transaction
end

END
Go to Top of Page
   

- Advertisement -