Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Next Id without Identity

 
   Database Help (Home) -> Programming RSS
Next:  How to calculate column percentage  
Author Message
Peter Ericsson

External


Since: Dec 06, 2006
Posts: 1



(Msg. 1) Posted: Wed Dec 06, 2006 8:49 am
Post subject: Next Id without Identity
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I'm getting a duplicate key problem with this code. I can't use a Identity
column.

BEGIN TRANSACTION
INSERT INTO T1 (Id1, Id2, OrderNo, Data)
SELECT @Id1_p, @Id2_p, ISNULL(max(OrderNo),0) + 1, @Data
FROM T1
WHERE Id1 = @Id1_p AND Id2 = @Id2_p

SELECT @iOrderNo = max(OrderNo)
FROM T1
WHERE Id1 = @Id1_p AND Id2 = @Id2_p
COMMIT TRANSACTION


Primary Key = (Id1, Id2, OrderNo)

 >> Stay informed about: Next Id without Identity 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 2) Posted: Thu Dec 07, 2006 8:09 am
Post subject: Re: Next Id without Identity [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Peter

BEGIN TRANSACTION

SELECT @iOrderNo = ISNULL(max(OrderNo),0) + 1 FROM T1 WITH
(UPDLOCK,HOLDLOCK)
WHERE Id1 = @Id1_p AND Id2 = @Id2_p

INSERT INTO T1 (Id1, Id2, OrderNo, Data) SELECT @Id1_p, @Id2_p,
iOrderNo, @Data

COMMIT TRANSACTION
>

"Peter Ericsson" wrote in message

> I'm getting a duplicate key problem with this code. I can't use a Identity
> column.
>
> BEGIN TRANSACTION
> INSERT INTO T1 (Id1, Id2, OrderNo, Data)
> SELECT @Id1_p, @Id2_p, ISNULL(max(OrderNo),0) + 1, @Data
> FROM T1
> WHERE Id1 = @Id1_p AND Id2 = @Id2_p
>
> SELECT @iOrderNo = max(OrderNo)
> FROM T1
> WHERE Id1 = @Id1_p AND Id2 = @Id2_p
> COMMIT TRANSACTION
>
>
> Primary Key = (Id1, Id2, OrderNo)

 >> Stay informed about: Next Id without Identity 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Identity Column - Is it possible to set the indentitiy flag for a column by simply using alter table/alter column in SQL 2000 and SQL 2005.

INSERT identity - If I issue a command to insert a record with ALLOW IDENTITY INSERT will I have to reset the current IDENTITY column? Thanks. David

How to find next identity - Hi All, Is there any way to know the next identity number the server would probably assign for the identity column? I'm having a table say Student with an identity column as StudentId. How to find the next Id value the system would assign for a new..

Question about IDENTITY columns. - We have a atble TAB1[ ID IDENTITY integer, NAME VARCHAR[30], .... ]. This table is present in 2 databases Db1 and Db2. We want to keep them both in sync daily. The mapping of Name -> ID should be the same in both the tables. We allow only Db1 tables t...

Identity/Seed Values - I have a question regarding using the Identity/Seed values. I have migrated a number of tables form Access to SQL. In Access the identity/seed values were called autonumbers. The big problem with autonumbers in Access was that they could change at any...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]