dbTalk Databases Forums  

is this possible

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss is this possible in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
rodchar
 
Posts: n/a

Default is this possible - 04-15-2005 , 09:19 AM






hey all,

i have a transaction table i need to add a record to. the primary key is a 2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar


Reply With Quote
  #2  
Old   
Adam Machanic
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 09:24 AM






Why not use an IDENTITY column?


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--


"rodchar" <rodchar (AT) discussions (DOT) microsoft.com> wrote

Quote:
hey all,

i have a transaction table i need to add a record to. the primary key is a
2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar




Reply With Quote
  #3  
Old   
Thomas
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 09:31 AM



Again provide DDL...

Create Table (RecordId Int, SequenceId Int
, Constraint Primary Key (RecordId, SequenceId))

Insert Table(RecordId, SequenceId)
Select RecordId, Max(SequenceId) + 1
From Table
Group By RecordId

BTW, this does have issues in a multi-user environment. If two people were to
execute this function at exactly the same time, they'll get the same answer and
thus a collision. A better way would be to make a small table that stores the
last value used.


Thomas



"rodchar" <rodchar (AT) discussions (DOT) microsoft.com> wrote

Quote:
hey all,

i have a transaction table i need to add a record to. the primary key is a 2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar




Reply With Quote
  #4  
Old   
Adam Machanic
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 09:40 AM



"Thomas" <thomas (AT) newsgroup (DOT) nospam> wrote

Quote:
BTW, this does have issues in a multi-user environment. If two people were
to
execute this function at exactly the same time, they'll get the same
answer and
thus a collision. A better way would be to make a small table that stores
the
last value used.
...which would have the same issue -- what would stop two readers from
getting the value simultaneously?


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--




Reply With Quote
  #5  
Old   
rodchar
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 09:55 AM



The problem is that this is an existing table in production.

"Adam Machanic" wrote:

Quote:
Why not use an IDENTITY column?


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--


"rodchar" <rodchar (AT) discussions (DOT) microsoft.com> wrote in message
news:88DBED0D-2707-4B78-AB8A-405F6E56F259 (AT) microsoft (DOT) com...
hey all,

i have a transaction table i need to add a record to. the primary key is a
2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar





Reply With Quote
  #6  
Old   
CBretana
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 10:18 AM



Just increase Isolation Leel to Repeatable Read (or Serializeable) to prevent
this issue from arising....

"Thomas" wrote:

Quote:
Again provide DDL...

Create Table (RecordId Int, SequenceId Int
, Constraint Primary Key (RecordId, SequenceId))

Insert Table(RecordId, SequenceId)
Select RecordId, Max(SequenceId) + 1
From Table
Group By RecordId

BTW, this does have issues in a multi-user environment. If two people were to
execute this function at exactly the same time, they'll get the same answer and
thus a collision. A better way would be to make a small table that stores the
last value used.


Thomas



"rodchar" <rodchar (AT) discussions (DOT) microsoft.com> wrote in message
news:88DBED0D-2707-4B78-AB8A-405F6E56F259 (AT) microsoft (DOT) com...
hey all,

i have a transaction table i need to add a record to. the primary key is a 2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar





Reply With Quote
  #7  
Old   
Thomas
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 10:25 AM



If you use a small table that stores the next value, you can lock the table and
increment the "next" value. In essence, serializing the retrieval of the next id
value. However, it does mean you may get gaps.


Thomas


"Adam Machanic" <amachanic (AT) hotmail (DOT) _removetoemail_.com> wrote

Quote:
"Thomas" <thomas (AT) newsgroup (DOT) nospam> wrote in message
news:OuVOPfcQFHA.3496 (AT) TK2MSFTNGP09 (DOT) phx.gbl...

BTW, this does have issues in a multi-user environment. If two people were
to
execute this function at exactly the same time, they'll get the same
answer and
thus a collision. A better way would be to make a small table that stores
the
last value used.

...which would have the same issue -- what would stop two readers from
getting the value simultaneously?


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--





Reply With Quote
  #8  
Old   
Thomas
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 10:25 AM



If you can withstand the entire table being locked during the insert process,
this would also be a viable choice.


Thomas

"CBretana" <cbretana (AT) areteIndNOSPAM (DOT) com> wrote

Quote:
Just increase Isolation Leel to Repeatable Read (or Serializeable) to prevent
this issue from arising....

"Thomas" wrote:

Again provide DDL...

Create Table (RecordId Int, SequenceId Int
, Constraint Primary Key (RecordId, SequenceId))

Insert Table(RecordId, SequenceId)
Select RecordId, Max(SequenceId) + 1
From Table
Group By RecordId

BTW, this does have issues in a multi-user environment. If two people were to
execute this function at exactly the same time, they'll get the same answer
and
thus a collision. A better way would be to make a small table that stores the
last value used.


Thomas



"rodchar" <rodchar (AT) discussions (DOT) microsoft.com> wrote in message
news:88DBED0D-2707-4B78-AB8A-405F6E56F259 (AT) microsoft (DOT) com...
hey all,

i have a transaction table i need to add a record to. the primary key is a
2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar







Reply With Quote
  #9  
Old   
Adam Machanic
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 10:29 AM



"CBretana" <cbretana (AT) areteIndNOSPAM (DOT) com> wrote

Quote:
Just increase Isolation Leel to Repeatable Read (or Serializeable) to
prevent
this issue from arising....
How would that prevent issues?

QA Window 1:
----------------

use tempdb
go

create table x(id int)
go

insert x values (1)
go

set transaction isolation level serializable
go

begin tran

select id
from x
go



QA Window 2:
---------------
use tempdb
go

set transaction isolation level serializable
go

begin tran

select id
from x
go


Serializable blocks only if writes have taken place.


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--


Quote:
"Thomas" wrote:

Again provide DDL...

Create Table (RecordId Int, SequenceId Int
, Constraint Primary Key (RecordId, SequenceId))

Insert Table(RecordId, SequenceId)
Select RecordId, Max(SequenceId) + 1
From Table
Group By RecordId

BTW, this does have issues in a multi-user environment. If two people
were to
execute this function at exactly the same time, they'll get the same
answer and
thus a collision. A better way would be to make a small table that
stores the
last value used.


Thomas



"rodchar" <rodchar (AT) discussions (DOT) microsoft.com> wrote in message
news:88DBED0D-2707-4B78-AB8A-405F6E56F259 (AT) microsoft (DOT) com...
hey all,

i have a transaction table i need to add a record to. the primary key
is a 2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar







Reply With Quote
  #10  
Old   
CBretana
 
Posts: n/a

Default Re: is this possible - 04-15-2005 , 11:11 AM



Adam,

Yes with just a read, but if you use the relation from t he select as
Insert values, you are doing more than just a read, and qry window 2 will
block..

create table x(id int)
go

insert x values (1)
go

set transaction isolation level serializable
go

begin tran

Insert x (id)
select id + 1 from x


-- Wait here while you run Qry WIndow 2 -----

Commit Tran
-- ******************************


--Query Window 2
---------------------------
set transaction isolation level serializable
go

begin tran

Insert x (id)
select id + 1 from x
-- Now go back and commit Query Window 1
-- -------------------------------------------------
"Adam Machanic" wrote:

Quote:
"CBretana" <cbretana (AT) areteIndNOSPAM (DOT) com> wrote in message
news:BBBD444B-B41C-4C6D-8872-7BFDA0364971 (AT) microsoft (DOT) com...
Just increase Isolation Leel to Repeatable Read (or Serializeable) to
prevent
this issue from arising....

How would that prevent issues?

QA Window 1:
----------------

use tempdb
go

create table x(id int)
go

insert x values (1)
go

set transaction isolation level serializable
go

begin tran

select id
from x
go



QA Window 2:
---------------
use tempdb
go

set transaction isolation level serializable
go

begin tran

select id
from x
go


Serializable blocks only if writes have taken place.


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--



"Thomas" wrote:

Again provide DDL...

Create Table (RecordId Int, SequenceId Int
, Constraint Primary Key (RecordId, SequenceId))

Insert Table(RecordId, SequenceId)
Select RecordId, Max(SequenceId) + 1
From Table
Group By RecordId

BTW, this does have issues in a multi-user environment. If two people
were to
execute this function at exactly the same time, they'll get the same
answer and
thus a collision. A better way would be to make a small table that
stores the
last value used.


Thomas



"rodchar" <rodchar (AT) discussions (DOT) microsoft.com> wrote in message
news:88DBED0D-2707-4B78-AB8A-405F6E56F259 (AT) microsoft (DOT) com...
hey all,

i have a transaction table i need to add a record to. the primary key
is a 2
field column. RecordID and then the SequenceID.

How i understand it is:
To add a new record i have to
1. Find the last sequence number used
2. Then add the new record

can i do this in one stored procedure?

thanks,
rodchar








Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.