"CREATE TABLE test (" & _
"ID INTEGER DEFAULT GenUniqueID() NOT NULL," & _
" data_col INTEGER NOT NULL UNIQUE);"
Thanks Jamie. Haven't actually had to use this (I rarely create tables
in code) but it's good to know!
One question I've had about Random autonumbers: does the algorithm
that generates them check first for duplicates, or does it just count
on the low probability that a random 32-bit integer will be a
duplicate? The SQL Upsizing wizard generates the following trigger
(posted verbatim as generated by the wizard):
CREATE TRIGGER T_CONtblPeople_ITrig ON [CONtblPeople] FOR INSERT AS
SET NOCOUNT ON
DECLARE @randc int, @newc int /* FOR AUTONUMBER-EMULATION CODE */
/* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'ContactID' */
SELECT @randc = (SELECT convert(int, rand() * power(2, 30)))
SELECT @newc = (SELECT ContactID FROM inserted)
UPDATE CONtblPeople SET ContactID = @randc WHERE ContactID = @newc
I'm rather rusty at T-SQL but I don't see anything in this code that
either checks for duplicates, or retries with a new random number if a
primary key constraint gets violated. What's the trick?
John W. Vinson[MVP]