Creating a trigger to write to a field

Hi, I have a table with the following fields,
no.
firstname
middle name
lastname
date of birth
sex
title
NIN

I want to write a trigger to auto fill the field NIN Base o the sex and date of birth. the NIN format is as follows
DDMMYY-RR-SSS
where DDMMYY is date of birth
RR is region and which will be hard coded
SSS is a randomly generated number where the even are male and odd female. The number should be unique. No two employees can have the same number.
How can achieve this?
Thank you
AtourayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

indianguru2Commented:
Following is a skelton which will help you to achieve what you want...
You cannot use a trigger for Update ... since it will have call the trigger when you update the table from the trigger itself.

CREATE TRIGGER trg_UpdateNIN
ON YourTableName
FOR INSERT
AS
 
DECLARE @NIN VARCHAR(30)
DECLARE @SSS VARCHAR(10)
 
SET @SSS = '' --- Your Randomly generated Number here.
 
select  @NIN = date_of_birth + RR + @SSS FROM INSERTED 
 
UPDATE y
SET NIN = @NIN
FROM YourTableName y
INNER JOIN INSERTED i ON i.NO = y.no

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mfreudenCommented:
You can use the trigger for update as well if you check the recursion level at the start of the trigger:

IF ((
SELECT TRIGGER_NESTLEVEL()) > 1 )
RETURN

0
AtourayAuthor Commented:
SET @SSS = '' --- Your Randomly generated Number here.

Can you please explain further.. I just want the system to randanlomy generate numbers between 000 - 999 where even is male odd female. how can i achieve this?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

mfreudenCommented:
Primer on creating random numbers in tsql:

---- This will create a random number between 1 and 999
SET @Lower = 1 ---- The lowest random number
SET @Upper = 999 ---- The highest random number
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random

Add this in your case:

select @SSS = convert(varchar(20),@Random)
0
AtourayAuthor Commented:
Thanks. Allow me to try it and get back to you.
0
AtourayAuthor Commented:
Thanks alot.. I tried it and its working. How can i set this in a way that they will be no duplicate of these numbers. Because these numbers are suppose to be unique. and also i want it to auto run the script for each newly registered member.
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.