Last post Aug 20, 2010 04:18 PM by ignatandrei
Aug 20, 2010 02:28 PM|Ima Noob|LINK
I need to create an ID from two fields when they are entered into the db for the first time. I thought Uniqueidentifer would do this, but it looks like uniqueidentifier is random and i have no control of the process.
My user will enter 4 letters into a column called INIT and 4 numbers into a column called NUMB. What I would like to do is create an id by combining those fields.
How can I do this?
Aug 20, 2010 04:03 PM|Steelymar|LINK
You can use trigger to acheave this:
CREATE TABLE [dbo].[Table_1](
[id] [varchar](50) NOT NULL,
[INIT] [nchar](4) NULL,
[NUMB] [int] NULL
) ON [PRIMARY]
CREATE TRIGGER tr_AddID ON dbo.Table_1 INSTEAD OF INSERT
SET NOCOUNT ON;
insert into Table_1
select [INIT]+cast(NUMB as char(4)) as ID,[INIT],NUMB from inserted
Aug 20, 2010 04:18 PM|ignatandrei|LINK
I am not sure if the combination (INIT + NUMB) will be unique every time
You have several methods :
1. INIT+ NUMB + DateTime.Now.ToString("yyyyMMddHHmmss") + SessionID
2.INIT+ NUMB + newguid