1

I am trying to auto generate a document reference number when a new document is added to an sql table - the reference number is a concatonation of some of the other fields in that table.

Looking online i can see one method is to use the dbid function to generate a uid and then create a function to concatonate and then a trigger on the table on insert to populate the column, but ive spent numerous hours and i cant get it to work.

The table has the following columns:-

Table:-     dbo.codeallocations21322        

Columns :-
Dbid        
Projectcode 
Type        
Discipline]     
Hdlreference

So the hdlreference column would be populated with :-

[projectcode]-[type]-[discipline]-[bdid] 

With the [bdid] set to 6 characters'.

Eg 21322-rfq-mech-000001 Any help would be much appreciated / advise a better way ?

Many thanks in advance.

3
  • Is The Terrible Grammar A Scouse Thing? Commented Mar 30, 2017 at 14:32
  • I assume SQL-Server because dbo.. Please tag your DBMS in future Commented Mar 30, 2017 at 14:34
  • Thanks for the feedback @JohnHC - will heed your advice, first post. Cheers. Commented Mar 30, 2017 at 14:51

2 Answers 2

1

You can use a computed column

alter table codeallocations21322
  add hdlreference 
      as ( projectcode  + '-' + type + '-' + discipline + '-' + dbid);
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for that answer @JohnHC - altered it slightly to convert the dbID to a Varchar :-
0

Many thanks John - altered slightly to convert the Int dbID to a varchar and works great :-

alter table CodeAllocations21322

  add hdlreference2

    as (ProjectCode  + '-' + Type + '-' + Discipline + '-' + right('00000' + Cast (dbID AS varchar(5)), 5));

Thanks again, look forward to talking to you in the future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.