SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Generating a Sequential Pattern


Generating a Sequential Pattern

Author
Message
Mark Hickin
Mark Hickin
SSC-Addicted
SSC-Addicted (419 reputation)SSC-Addicted (419 reputation)SSC-Addicted (419 reputation)SSC-Addicted (419 reputation)SSC-Addicted (419 reputation)SSC-Addicted (419 reputation)SSC-Addicted (419 reputation)SSC-Addicted (419 reputation)

Group: General Forum Members
Points: 419 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.
ScottPletcher
ScottPletcher
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34989 Visits: 7732

>> "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) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Tatsu
Tatsu
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2826 Visits: 307

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
noeld
noeld
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38988 Visits: 2051

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
Bhushan
Bhushan
Say Hey Kid
Say Hey Kid (679 reputation)Say Hey Kid (679 reputation)Say Hey Kid (679 reputation)Say Hey Kid (679 reputation)Say Hey Kid (679 reputation)Say Hey Kid (679 reputation)Say Hey Kid (679 reputation)Say Hey Kid (679 reputation)

Group: General Forum Members
Points: 679 Visits: 96
Will it work in multiuser environment???



noeld
noeld
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38988 Visits: 2051
Absolutely


* Noel
Scott Coleman
Scott Coleman
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11641 Visits: 1587

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.





ScottPletcher
ScottPletcher
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34989 Visits: 7732

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) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Dennis D. Allen
Dennis D. Allen
Old Hand
Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)

Group: General Forum Members
Points: 319 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
Scott Coleman
Scott Coleman
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11641 Visits: 1587

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.





Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search