Click here to Skip to main content
15,902,908 members
Home / Discussions / Database
   

Database

 
AnswerRe: How to get max on each row Pin
xoxoxoxoxoxox26-Jan-07 10:09
xoxoxoxoxoxox26-Jan-07 10:09 
GeneralRe: How to get max on each row Pin
andyharman26-Jan-07 10:18
professionalandyharman26-Jan-07 10:18 
GeneralRe: How to get max on each row Pin
Michael Potter26-Jan-07 10:31
Michael Potter26-Jan-07 10:31 
GeneralRe: How to get max on each row [modified] Pin
xoxoxoxoxoxox26-Jan-07 11:08
xoxoxoxoxoxox26-Jan-07 11:08 
QuestionInt value as 0001 Pin
javierarka26-Jan-07 9:40
javierarka26-Jan-07 9:40 
AnswerRe: Int value as 0001 Pin
andyharman26-Jan-07 9:52
professionalandyharman26-Jan-07 9:52 
QuestionColumn ID in a Table wiht autoincrement value predetermined Pin
javierarka26-Jan-07 9:23
javierarka26-Jan-07 9:23 
AnswerRe: Column ID in a Table wiht autoincrement value predetermined Pin
andyharman26-Jan-07 9:50
professionalandyharman26-Jan-07 9:50 
Which database engine are you using? If you were using a SQL-Server stored procedure then the code would look something like:
create procedure MyRecordInsert
@Prefix varchar(3),
@OtherData varchar(20)
as begin
   set nocount on
   declare @PrimaryKey varchar(15), @MaxKey int
   Begin tran

   --Create first part of primary key (prefix + date).
   set @PrimaryKey = @Prefix + Convert(varchar(8), GetDate(), 112)

   --Find the higher number that has been allocated today.
   --Use locking to prevent anyone else inserting until we have finished
   --our transaction.
   select @MaxKey = Convert(Int, Substring(Max(PrimaryKey), 12, 4))
      from MyTable with (tablock, holdlock)
      where PrimaryKey like @PrimaryKey + '%'

   --Increment the highest number, and then append to key.
   set @MaxKey = IsNull(@MaxKey, 0) + 1
   set @PrimaryKey = PrimaryKey +
         Right('0000' + Convert(varchar(4), @MaxKey), 4)

   --Insert record into the database.
   insert into MyTable (PrimaryKey, OtherData)
      values (@PrimaryKey, @OtherData

   Commit tran
end
You will need to add some error handling. The general approach is valid for other database engines (although the syntax would obviously be different). The combination of a transaction and locks should allow you to allocate new keys without clashing with other users. If you only have a small number of users then you might be able to get away with a simpler approach.

Hope this is helpful.
Andy
AnswerRe: Column ID in a Table wiht autoincrement value predetermined Pin
Pete O'Hanlon26-Jan-07 10:05
mvePete O'Hanlon26-Jan-07 10:05 
QuestionCorrect way of databinding of foreign key? Pin
TrooperIronMan26-Jan-07 8:32
TrooperIronMan26-Jan-07 8:32 
QuestionUpdate Query using more than one table Pin
sayianjin26-Jan-07 5:55
sayianjin26-Jan-07 5:55 
AnswerRe: Update Query using more than one table Pin
andyharman26-Jan-07 7:25
professionalandyharman26-Jan-07 7:25 
QuestionCross databae table relation ship Pin
TUX2K25-Jan-07 23:37
TUX2K25-Jan-07 23:37 
QuestionConvert getdate() Pin
javierarka25-Jan-07 10:59
javierarka25-Jan-07 10:59 
AnswerRe: Convert getdate() Pin
andyharman25-Jan-07 22:49
professionalandyharman25-Jan-07 22:49 
AnswerRe: Convert getdate() Pin
Szemi9025-Jan-07 22:59
Szemi9025-Jan-07 22:59 
QuestionCDaoDatabase Create Field Pin
DKScherpy25-Jan-07 9:58
DKScherpy25-Jan-07 9:58 
QuestionIncrement Field Value Pin
achimera25-Jan-07 9:39
achimera25-Jan-07 9:39 
AnswerRe: Increment Field Value Pin
PIEBALDconsult25-Jan-07 9:55
mvePIEBALDconsult25-Jan-07 9:55 
GeneralRe: Increment Field Value Pin
achimera25-Jan-07 10:06
achimera25-Jan-07 10:06 
Questioncall function from select join Pin
montu337725-Jan-07 3:51
montu337725-Jan-07 3:51 
AnswerRe: call function from select join Pin
Paddy Boyd25-Jan-07 4:29
Paddy Boyd25-Jan-07 4:29 
QuestionSCOPE_IDENTITY() Always Returning 1 Pin
Farm Developer25-Jan-07 0:43
Farm Developer25-Jan-07 0:43 
AnswerRe: SCOPE_IDENTITY() Always Returning 1 Pin
Jon Sagara25-Jan-07 10:23
Jon Sagara25-Jan-07 10:23 
GeneralRe: SCOPE_IDENTITY() Always Returning 1 Pin
Farm Developer25-Jan-07 21:42
Farm Developer25-Jan-07 21:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.