![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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 |
#6
| |||
| |||
|
|
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 |
#7
| |||
| |||
|
|
"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 -- |
#8
| |||
| |||
|
|
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 |
#9
| |||
| |||
|
|
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 |
#10
| |||
| |||
|
|
"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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |