StarCub
StarCub

Reputation: 4341

SQL Server - formatted identity column

I would like to have a primary key column in a table that is formatted as FOO-BAR-[identity number], for example:

FOO-BAR-1  
FOO-BAR-2  
FOO-BAR-3  
FOO-BAR-4  
FOO-BAR-5  

Can SQL Server do this? Or do I have to use C# to manage the sequence? If that's the case, how can I get the next [identity number] part using EntityFramwork?

Thanks

EDIT:

I needed to do this is because this column represents a unique identifier of a notice send out to customers.

So is it better to have just an int identity column and append the values in Business Logic Layer in C#?

Upvotes: 0

Views: 2799

Answers (4)

Qcpbraca
Qcpbraca

Reputation: 91

It is quite possible by using computed column like this:

CREATE TABLE #test (
    id INT IDENTITY UNIQUE CLUSTERED,
    pk AS CONCAT('FOO-BAR-', id) PERSISTED PRIMARY KEY NONCLUSTERED,
    name NVARCHAR(20)
)

INSERT INTO #test (name) VALUES (N'one'), (N'two'), (N'three')

SELECT id, pk, name FROM #test

DROP TABLE #test

Note that pk is set to NONCLUSTERED on purpose because it is of VARCHAR type, while the IDENTITY field, which will be unique anyway, is set to UNIQUE CLUSTERED.

Upvotes: 0

Alex_L
Alex_L

Reputation: 2666

If you want this 'composited' field in your reports, I propose you to:

  1. Use INT IDENTITY field as PK in table
  2. Create view for this table. In this view you can additionally generate the field that you want using your strings and types.
  3. Use this view in your repoorts.

But I still think, that there is BIG problem with DB design. I hope you'll try to redesign using normalization.

Upvotes: 3

Tim
Tim

Reputation: 28520

Another option would be to use what an old team I used to be on called a codes and value table. We didn't use it for precisely this (we used it in lieu of auto-incrementing identities to prevent environment mismatches for some key tables), but what you could do is this:

  1. Create a table that has a row for each of your categories. Two (or more) columns in the row - minimum of category name and next number.
  2. When you insert a record in the other table, you'll run a stored proc to get the next available identity number for that category, increment the number in the codes and values table by 1, and concatenate the category and number together for your insert.

However, if you're main table is a high-volume table with lots of inserts, it's possible you could wind up with stuff out of sequence.

In any event, even if it's not high volume, I think you'd be better off to reexamine why you want to do this, and see if there's another, better way to do it (such as having the business layer or UI do it, as others have suggested).

Upvotes: 0

Tim Meers
Tim Meers

Reputation: 928

You can set anything as the PK in a table. But in this instance I would set IDENTITY to just an auto-incrementing int and manually be appending FOO-BAR- to it in the SQL, BLL, or UI depending on why it's being used. If there is a business reason for FOO and BAR then you should also set these as values in your DB row. You can then create a key in the DB between the two three columns depending on why your actually using the values.

But IMO I really don't think there is ever a real reason to concatenate an ID in such a fashion and store it as such in the DB. But then again I really only use an int as my ID's.

Upvotes: 1

Related Questions