2

Hello stackOverflowers !

i was wondering if theres a way to get in a safe way, series of numbers in transactions just like the identity.My only purpose is for grouping rows in tables and i don't mean row_number(). i've came up with this simple query, is this safe? this table has its own identity key

declare @mynextSecuenceNumber int
    select @mynextSecuenceNumber=isnull(max(secuencenumber+1),1) from mytable
insert into mytable (productID,customer,secuencenumber) values (@someval,@anotherval,@mynextSecuenceNumber)

EDIT

THE BACKGROUND

the reason for doing this is the next:

first i'm recieving autoparts for car services then i generate a ticket for that recepcion(i can recieve one,two,three auto parts) later on i can continue on reciving autoparts for that specific car service from the same autopart provider or from a different provider but i want to be able to re generate the event or the ticket otherwise i'll end up querying the service and all the the autoparts associated with that or the provider and i wont know the event what i recived in that operation and on top of that i need another specific id for all those autoparts associated with that car service.

by the way i'm on sql server 2008

heads up

using identity as secuence number can be messy cus transactions will increment the value after rolling back and other issues so be aware of that thanks to the approach privided as my acepted answer i can find another way who gets along with transactions its the first to appear on the link

Community
  • 1
  • 1
GoAntonio
  • 183
  • 2
  • 13
  • 2
    There are so many challenges of rolling your own identity. Why not just use the identity? It has already handled all those challenges. – Sean Lange Sep 30 '15 at 15:56
  • With fields like `productID, customer` its hard to see why sequentially incrementing numbers are necessary at all? As commented above an identity value is numeric, increasing and unique, use one if you can. – Alex K. Sep 30 '15 at 16:24
  • 1
    yeah i'll use a table just for identity numbering before i move to sql server 2012 with the next CREATE SEQUENCE and the NEXT VALUE FOR function – GoAntonio Sep 30 '15 at 17:07
  • You could always use a uniqueidentifier ( Guid ) and then use that as your PK to keep everything associated. Sounds like you are receiving a PO, where the PO number would be your GUID and the the PO Details would be the indentity field with the PO Guid as the FK on the PO Details. – ewahner Sep 30 '15 at 17:11
  • I would stay away from the guid unless you had a real reason for it. 16 byte keys take up lots of space for indexes compared to numeric fields, and most people never consider the fragmentation unless sequential guids are used. – Jason W Sep 30 '15 at 17:13

1 Answers1

1

Here's a scalable recommendation from Microsoft when SQL 2012 or higher isn't an option, but you need to manage sequence numbers without identities in the target table. This creates a separate table to track the sequence numbers, let's identity do some of the heavy lifting, and keeps the table size minimal by cleaning up. If this load becomes too much, you can schedule something for the cleanup during off-peak time.

-- Create table for tracking the sequence
create table <tablename> (
      SeqID int identity(1,1) primary key
)
GO

-- Create procedure to return next sequence value
create procedure GetNewSeqVal_<tablename>
    @NextValue int output
as
begin
    declare @NewSeqValue int
    set nocount on
    insert into <tablename> DEFAULT VALUES
    set @NewSeqValue = scope_identity()
    delete from <tablename> with (readpast)
    set @NextValue = @NewSeqValue
end
go

-- Get next sequence
declare @seqId int
exec GetNewSeqVal_<tablename> @NextValue = @seqId OUTPUT

For more info: http://blogs.msdn.com/b/sqlcat/archive/2006/04/10/sql-server-sequence-number.aspx

Jason W
  • 13,026
  • 3
  • 31
  • 62
  • oh sir i love this! nice approach and explanation i don't even need to migrate BUT why deleting the table will get me the next time a consecutive id? do this means that i need to create the table again after executing the procedure? – GoAntonio Sep 30 '15 at 17:54
  • 1
    To jump-start your research, this only deletes the record in your table. However, the identity value is not reset (that only happens if the table is recreated or truncated or dbcc checkident), so it still gets the next sequence number on the next call. – Jason W Sep 30 '15 at 17:58
  • oh those tricky DBA magic spells thank you a lot now i'm ready to go – GoAntonio Sep 30 '15 at 18:02
  • 1
    @GoAntonio fyi: There was an issue with `SCOPE_IDENTITY` in earlier versions of SQL Server as described [here](https://support.microsoft.com/en-us/kb/2019779). (The [Microsoft Connect article](https://connect.microsoft.com/SQLServer/feedback/details/328811/scope-identity-sometimes-returns-incorrect-value) seems to have gone missing.) Make sure that any required updates are installed or, if practical, use an `OUTPUT` clause. – HABO Sep 30 '15 at 22:20
  • thank you mate i'm aware thanks to mr.pinal dave he is so awesome! at here http://blog.sqlauthority.com/2009/03/24/sql-server-2008-scope_identity-bug-with-multi-processor-parallel-plan-and-solution/ now heres the solution https://support.microsoft.com/en-us/kb/2019779 for those ! – GoAntonio Sep 30 '15 at 22:50