October 19, 2018 at 3:23 am
Hi,
I have a table with column [CustomerID] [int] IDENTITY(1,1).
I would like to create a fixed width 'user friendly' refernec number e.g. CN000001, CN000002 etc...
It can be done in a VIEW ... but id like in the main table.
This got me wondering what the best way to do this might be.
The trigger below works ... but is there a trick im missing with Identity, computed columns etc... or a more efficent trigger?
Any advice welcomed
Thanks
/*
Triggers uses column ([CustomerID] [int] IDENTITY(1,1) )
and creates a user friendly Customer Number e.g. CN000123
*/
CREATE TRIGGER [tr_tDeals_cnCustID] ON [FedCapDB_SQL_TEST].[dbo].[tCustomer]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE [tCustomer]
SET cnCustomerID = 'CN' + RIGHT('00000' + CAST(t.[CustomerID] AS VARCHAR(8)), 5)
FROM [tCustomer] t
WHERE t.cnCustomerID IS NULL
END
October 19, 2018 at 4:54 am
Why not use a PERSISTED Computed Column? In a very simple sense:
This would probably be far easier than a trigger.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
October 19, 2018 at 5:54 am
This is perfect - seem slicker than a Trigger.
This is just the kind of reply i was looking for.
Many Thanks
Dan
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply