Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL Primary Key Generation using triggers

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-14 : 09:25:52
Puneet writes "I want to generate a 13 digit alphanumeric Primary Key using a Trigger (Value like ZD10000000001)
The Problem is I want to take a maximum value and increment it by 1 each time the table is updated.
I am facing the problem that each time before updating transacting the table the Primary key contains Null value which is not allowed in a Primary Key.

Tell me a solution for that"

Jay99

468 Posts

Posted - 2002-03-14 : 09:29:11
quote:

Tell me a solution for that




Triggers in SQL 7 are exclusivly 'AFTER' Triggers. In SQL 2k you can have an 'INSTEAD OF' . . .

Jay
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-03-14 : 09:48:08
I wouldn't recommend doing this via triggers even in SQL2k (as Jay pointed out, in SQL7 it ain't even gonna be an option for ya). What you need to do is to create a service table that will contain the last used key value. And then write a stored proc that will increment and retrieve the value from that table for you whenever you need one for your insert.

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-03-14 : 10:52:30
What happens if you put a default on the field?
Make the default something that cannot be generated.

==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -