Incrementing surrogate key

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
bhargav_dd
Premium Member
Premium Member
Posts: 57
Joined: Tue Jun 30, 2009 9:38 am

Incrementing surrogate key

Post by bhargav_dd »

Hi,

I have a flat file which will be loaded into DB tables
everyday.Suppose for example, if I get 10 records today in the
flatfile,I need to assign a Surrogate Key to these records and
load it into the DB table.So next day, when I get new set of
records I need to track the highest Surrogate Value in the DB
table and then assign surrgate key values to these new records
starting from the highest value.

I am new to data Stage and am struggling to implement this
logic.Can any one of you give explain how it can implemented in
Server Edition?
piyu
Participant
Posts: 52
Joined: Thu Jul 21, 2005 12:22 am
Location: Singapore

Post by piyu »

In 7x you can do it two ways : get the max value from the table, and have it incremented in the transformer for each row being inserted, OR always store the max value in a text file on unix and have it exported to the job environment.

8x provides the functionality in the surrogate key stage.
vivekgadwal
Premium Member
Premium Member
Posts: 457
Joined: Tue Sep 25, 2007 4:05 pm

Re: Incrementing surrogate key

Post by vivekgadwal »

You could also use the Key Management utility routines provided by DataStage. Look for "KeyMgmt..." routines (unsure about the name) and you need to provide a parameter to them (a unique one which will keep track of the previous SK values generated).
Vivek Gadwal

Experience is what you get when you didn't get what you wanted
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

The biggest question you need to answer regards concurreny - will your load run in isolation or could others be loading into the target as well? That will drive your available strategies.
-craig

"You can never have too many knives" -- Logan Nine Fingers
gateleys
Premium Member
Premium Member
Posts: 992
Joined: Mon Aug 08, 2005 5:08 pm
Location: USA

Re: Incrementing surrogate key

Post by gateleys »

Use the built-in transform, KeyMgtGetNextValue('YourSequenceName'). In case there is concurrent writing to the same table, you could use KeyMgtGetNextValueConcurrent('YourSequenceName')
gateleys
natashab
Participant
Posts: 17
Joined: Mon Aug 09, 2010 3:44 am

Surrogate Key

Post by natashab »

Design the job as shown below:
------------------Surrogate table


FlatFile -------> Lookup ---------->Transformer---------> DB2

............................... ..................... Aggregator--------->Surrogate Table

In the lookup stage ,lookup the table_name(target table name) field and retrieve the Max_val(from surrogate table)for that table_name.

The transformer stage will load data to the target db2 table through one link where it will generate unique key values for the primary key using @rownum+1 function and through the other link max_val data for that particular target table_name will be loaded to the surrogate table via the aggregator stage .In the aggregator stage get the maximum value for the surrogate/primary key field.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Which would certainly work... or you could use one of the noted 'Key Management' routines, save yourself some of the shenanigans. :wink:
-craig

"You can never have too many knives" -- Logan Nine Fingers
arunkumarmm
Participant
Posts: 246
Joined: Mon Jun 30, 2008 3:22 am
Location: New York
Contact:

Re: Surrogate Key

Post by arunkumarmm »

natashab wrote:Design the job as shown below:
------------------Surrogate table


FlatFile -------> Lookup ---------->Transformer---------> DB2

............................... ..................... Aggregator--------->Surrogate Table

In the lookup stage ,lookup the table_name(target table name) field and retrieve the Max_val(from surrogate table)for that table_name.

The transformer stage will load data to the target db2 table through one link where it will generate unique key values for the primary key using @rownum+1 function and through the other link max_val data for that particular target table_name will be loaded to the surrogate table via the aggregator stage .In the aggregator stage get the maximum value for the surrogate/primary key field.

Thought it was a server job...
Arun
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

It is... just seems to be a badly drawn one. :wink:

And even if they meant a PX design, it is easy enough to do the equivalent in Server with a direct reference lookup off the Transformer, although I would use a hashed file for that.
-craig

"You can never have too many knives" -- Logan Nine Fingers
natashab
Participant
Posts: 17
Joined: Mon Aug 09, 2010 3:44 am

RE

Post by natashab »

The page didn't allow me to draw the design of the job perfectly.
That's PX job design.
If it's a server job then a hash file can be used to do the lookup.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Yah, ASCII Art here can be a little challenging as the forum software automagically removes whitespace from everything that is not wrapped in code tags. So what you need to do is use the code tags and then liberally Preview what your output is going to look like in order to get it right. It won't line up like you think it will due to the proportional font used here, so it will need to look a little goofy on your side to line up after it has been posted.
-craig

"You can never have too many knives" -- Logan Nine Fingers
Post Reply