Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Generating a Sequential Pattern Expand / Collapse
Author
Message
Posted Wednesday, May 17, 2006 8:55 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, November 24, 2011 2:31 AM
Points: 169, Visits: 156
Or better yet

update SequenceTable
set @NewSequenceNumber = CurrentSequenceNumber = CurrentSequenceNumber + 1
where SequenceYear = year(getdate())

Having said that, central locations for keys are a concurrency nightmare for anything that needs to scale. I would generally steer clear.
Post #280731
Posted Wednesday, May 17, 2006 8:59 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:07 PM
Points: 2,194, Visits: 3,304

>> "SET IDENTITY_INSERT ON" would be needed for prior year, with logic similar to above, but hopefully that would be relatively rare. <<

In fact, in most systems, especially dealing with invoices, it should never be allowed -- once the current year is past, no new invoices with the prior yr on them should be created.



SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #280735
Posted Wednesday, May 17, 2006 9:23 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, October 1, 2014 1:56 PM
Points: 295, Visits: 281

Mark,
Where are you updating the stored value? I have used this a few times and have not seen any impact on scalability but it may not have been under a big enough load. The statement as I posted it gets in and out of the table very quickly so I am not sure what kind of concurrency problems you might be referring to. Please give us a little more detail before I use this again!

Scott,
I like your idea as well. It wouldn't be hard to create a job that ran at the beginning of every year to reset the seed and increment of the identity column. The only downside (very minor imo) would be that you can't put any non-numeric characters in the field itself.



Bryant E. Byrd, BSSE MCDBA MCAD
Business Intelligence Administrator
MSBI Administration Blog
Post #280754
Posted Wednesday, May 17, 2006 9:42 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, October 16, 2014 6:06 AM
Points: 6,259, Visits: 2,030

First.

The Idea of a Sequence table is the right idea in this case.

Second.

"Central location for keys is a concurrency nightmare" if you are not using the appropriate locking mechanisms.

ex:

  • do not compute the sequence year on the query
  • Should you need more than one at a time add a @count param and get only the latest
  • if you have multiple sequences make sure you have a primary key on the sequencing key
  • should the table grow make sure that the update is using (with rowlock)
  • Last but not least if the number of proccess competing for keys is very ( and I mean very ) high use sp_tableoption and "pin" the sequence table.

As you can see I have experience with this structure and have been successfuly able to implemet such construct.

It is not my preffered way but it is very portable and very flexible

Cheers,

 

 




* Noel
Post #280761
Posted Wednesday, May 17, 2006 9:51 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 20, 2014 7:03 AM
Points: 131, Visits: 94
Will it work in multiuser environment???


Post #280769
Posted Wednesday, May 17, 2006 9:58 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, October 16, 2014 6:06 AM
Points: 6,259, Visits: 2,030
Absolutely


* Noel
Post #280773
Posted Wednesday, May 17, 2006 10:06 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, September 30, 2014 4:29 PM
Points: 2,844, Visits: 1,155

Noel, you're confusing me.  "A sequence table is the right idea", but "It is not my preferred way"?  So there's another way you prefer that's better than the ones posted, but you're not going to share it with us?

I agree that the sequence table is a very workable solution and would not worry about scaling if it is done right.  My biggest concern would be guaranteeing that all inserts are done by a stored procedure that correctly accesses the sequence table.

You could argue that the identity field approach should scale better because you only need one action to insert the record, while the sequence table requires an extra action for the update.  This is probably just a theoretical argument, because I can't imagine a real situation where a sequence table approach would break down if all of Noel's suggestions were applied.

On the other hand if you've got such a volume of invoices that you're worried about scaling, you should be able to afford enough hardware to deal with it.  I'd be happy to come help set up your new 32-processor servers for the reasonable sum of twice my current salary.




Post #280780
Posted Wednesday, May 17, 2006 10:21 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:07 PM
Points: 2,194, Visits: 3,304

One of my concerns with a sequence table, aside from the additional I/O and other overhead, is that you cannot then easily insert multiple invoices in one statement. 

If, for example, thru some EDI (yes, it is still used) or other process you have a batch of invoices to enter, you must single-step thru them with a sequence table, but not with an identity column.

 

As for a non-numeric, that's true, it could not be in the column itself -- then again, it shouldn't be anyway imo.  It should be a separate column and concatenated into the computed column that is displayed to the user.

 



SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #280792
Posted Wednesday, May 17, 2006 10:29 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, June 5, 2012 12:03 PM
Points: 117, Visits: 163
I love functions... below is the function declaration but first the insert statment


INSERT INTO dbo.Invoice ( Invoice_id, Customer_name, Invoice_amount )
VALUES ( dbo.fn_NextKey(05), 'ACME Corp.', 12345.67 )

This technique also works with SELECT inserts for multi-row inserting joy. The function is also easy to modify for different key compositions mmyy-nnnnnnnnnn or what ever...

Cheers!


CREATE FUNCTION dbo.fn_NextKey( @year int )
RETURNS varchar(10)
AS
BEGIN

/* ASSERTIONS
* Invoice_Id has a fixed pattern of yy-nnnnnn where yy is last two digits of a year
* and nnnnnn is a 6 digit 0 padded number that is incremented by 1
*/
DECLARE @pattern char(3), @answer varchar(10)

SET @pattern = right('00'+convert(varchar(10),@year),2) + '-'

SELECT
@answer = CASE WHEN max(Invoice_id) IS NULL
THEN @pattern + '000001' /* first invoice of given year */
ELSE @pattern + right('000000'+convert(varchar(10),convert(int,substring(max(Invoice_id),4,6))+1),6)
END
FROM dbo.Invoice
WHERE Invoice_id LIKE @pattern+'%'

RETURN @answer

END
Post #280793
Posted Wednesday, May 17, 2006 10:54 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, September 30, 2014 4:29 PM
Points: 2,844, Visits: 1,155

I'm not sure how the function will work with multiple rows, I get a duplicate key error when I try it.

Maybe if it was implemented as a CLR function and flagged as non-deterministic, but I don't think it's worth the trouble.




Post #280805
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse