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

Autonumber on a field that doesn' have increlent or identity Expand / Collapse
Author
Message
Posted Monday, November 05, 2012 9:19 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, March 27, 2013 8:55 AM
Points: 289, Visits: 683
Hello all,
Quick question
I got a table with a varchar(20) field as primary key
the result has to be
W0001
W0002
....

The data is coming from existing tables and I'm using a merge statement to see wheiter the record is already present or not
Anyone got an idea how to create the autoincrement
Cheers
Resender
Post #1381149
Posted Monday, November 05, 2012 9:38 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:34 PM
Points: 8,592, Visits: 8,233
Resender (11/5/2012)
Hello all,
Quick question
I got a table with a varchar(20) field as primary key
the result has to be
W0001
W0002
....

The data is coming from existing tables and I'm using a merge statement to see wheiter the record is already present or not
Anyone got an idea how to create the autoincrement
Cheers
Resender


This question comes up around here at least once a week. It is not a good design at all. You have to make your key a varchar to do this and you have effectively limited the table to a max of 9,999 rows. What happens after W9999? does it become W10000? You can't order them correctly now because varchars sort differently than numbers.

;with cte as
(
select 'W1000' as KeyField union all
select 'W9999' union all
select 'W10000'
)

select KeyField
from cte
order by KeyField

What are you hoping to gain from this?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1381168
Posted Tuesday, November 06, 2012 12:41 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, March 27, 2013 8:55 AM
Points: 289, Visits: 683
//Brilliant I made the post with my text not appearing so here goes the edit

The scenario is a bit more complicated then I initially described, we have to 'merge' data from several databases to a single one.
All the databases are SQL Server databases and all of them are the source databases of ERP programs (so bye bye logic on that part), the destination database is the source for a Microsoft Dynamics Nav product. (So in my opinion a very poorly designed database but then the native database Dynamics used to run on was an OO-database type and Microsoft translated it to a relational one)


And I can't change the fields cause it's a root I don't have a license for to make any changes on(field changes & also any change I do can be undone by a product update in the future) , so I need a way to respect the Nav number series in the table while inserting the new data

When I mean 'merge' I'm not talking about the merge function but about the literal merging of the data of 2 or more sources, since the client I'm doing it for used a separate ERP program for sales then they did for invoices and inventories; and now they want to use a single one for the entire operation.
Post #1381420
Posted Tuesday, November 06, 2012 2:54 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 1:26 AM
Points: 2,340, Visits: 3,171
I agree with Sean on the dangers and issues with this. But if you must you must.

To get around the sorting issue he notes, there is always this:

;with cte as
(
select 'W1000' as KeyField union all
select 'W9999' union all
select 'W10000'
)

select KeyField
from cte
order by LEFT(KeyField, 1), CAST(RIGHT(KeyField, LEN(KeyField)-1) AS BIGINT)


And a somewhat awkward but potentially effective approach to generating new keys would be something like this:

;WITH cte as (
select 'W1000' as KeyField union all
select 'W9999' union all
select 'W10000'),
cte2 AS (
SELECT MaxN=MAX(CAST(RIGHT(KeyField, LEN(KeyField)-1) AS BIGINT))
FROM cte),
MyNewData (n) AS (
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3)
--INSERT INTO Table (KeyField)
SELECT 'W' + CAST(
n + (SELECT MaxN FROM cte2) -- Use ROW_NUMBER() instead of n if needed
AS VARCHAR(19))
FROM MyNewData


If this is a high transaction process inserting many new records constantly into the table, that may not work very well though. For that, you may need to SELECT with an UPDLOCK hint (the current max keyfield) within a transaction and then complete the transaction with your INSERT/MERGE.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1381462
Posted Tuesday, November 06, 2012 3:18 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, March 27, 2013 8:55 AM
Points: 289, Visits: 683
I actually agree with both of you but it's out of my hand, I'm limited by what the architecture allows me.
And I can't change the architecture, if I could I would
For instance the tables in the database are called in the following structure
databasename.dbo.companyname$tablename
I hope that that at least in the future it will be
databasename.companyname.tablename

As for the problem it's been resolved, I've been given permission to make a 'staging' table, the logic for the number series will then be done later in Nav itself when moving data to the main table.
Post #1381469
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse