Accessing a hash file with a key composed by several fields

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
gpbarsky
Participant
Posts: 160
Joined: Tue May 06, 2003 8:20 pm
Location: Argentina

Accessing a hash file with a key composed by several fields

Post by gpbarsky »

Hola mis amigos.... :D

I need to access a hash file from within a BASIC program. The key of the hash file is composed by several fields.

What is the BASIC sentence to write in the program, to read a record given all the fields of the key ?

Thanks in advance.
Guillermo P. Barsky
Buenos Aires - Argentina
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

OPENPATH will open an externally pathed hash file.

Code: Select all

OPENPATH fully_qualified_filename TO file_handle Else
    * put error in opening logic here
End
If you know the primary keys, construct a variable of all of the fields concatenated with an @TM character between each field. Ex:

KEY=field1:@TM:field2:@TM:field3

Code: Select all

READ ROW FROM file_handle, KEY Then
     * put found logic here
End Else
     * put not found logic here
End

The ROW variable will be a dynamic array of the data found at that KEY.
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
gpbarsky
Participant
Posts: 160
Joined: Tue May 06, 2003 8:20 pm
Location: Argentina

Post by gpbarsky »

Ken:

Thank you. This works perfectly well :D .

Thanks to this forum; it saved my work.

Bye.

:lol:
Guillermo P. Barsky
Buenos Aires - Argentina
phanee_k
Charter Member
Charter Member
Posts: 68
Joined: Thu Nov 20, 2003 11:02 pm

Post by phanee_k »

hI,
I modified in my routine as required.
I am using a two key hash file.
I am getting output as Record not Found.

I had used the
HashKey = Arg3:@TM:Arg5

where Arg3 and Arg5 are key columns data.
The routine is working fine with one argument.

Please help.

Regards
Phani
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Post your code.
-craig

"You can never have too many knives" -- Logan Nine Fingers
phanee_k
Charter Member
Charter Member
Posts: 68
Joined: Thu Nov 20, 2003 11:02 pm

Post by phanee_k »

Hi,
Please find my code below.


Deffun DSRMessage(A1, A2, A3) Calling "*DataStage*DSR_MESSAGE"

HashTable = Arg4:Arg1
HashKey = Arg2:@TM:Arg5
ColumnPosition = Arg3
PositionReturn = 0

* Determine if we are returning one column or entire row.
If Num(ColumnPosition) then
ColumnPosition = Int(ColumnPosition)
If ColumnPosition > 0 and ColumnPosition < 99999 Then
PositionReturn = 1
End
End

* Attempt to find the table name in our cache.
Locate HashTable in FilesOpened Setting POS Then
Read Rec From FileHandles(POS), HashKey Then
If PositionReturn Then Ans = Rec<ColumnPosition> Else Ans = Rec
End Else
Ans = DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" )
End
End Else
* Table is not in cache of opened tables, so open it.
OpenPath HashTable To FileHandles(POS) Then
FilesOpened<-1> = HashTable
Read Rec From FileHandles(POS), HashKey Else
Rec = DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" )
End
If PositionReturn And Rec <> DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" ) Then
Ans = Rec<ColumnPosition>
End Else
Ans = Rec
End
End Else
Rec = DSRMessage("DSTAGE_TRX_I_0011" , "**TABLE NOT FOUND**", "" )
Ans = Rec
End
End



regards,
Phani
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

The code's OK (but, next time, format it and surround it with CODE tags).

My guess is that the arguments and the key values actually in the hashed file do not coincide - maybe there are trailing spaces in the latter.

Make sure that the number of trailing spaces, leading zeroes, etc., is identical. Best practice it to TRIM when loading the hashed file, and to TRIM when preparing the key values for a reference lookup.

When testing the routine, you might contemplate logging informational messages to indicate:
(a) which "record not found" message was issued (there are two paths)
(b) the argument values (use the Quote() function to see exactly
(c) the key value that was used (use Convert(@TM,"|", HashKey))
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
phanee_k
Charter Member
Charter Member
Posts: 68
Joined: Thu Nov 20, 2003 11:02 pm

Post by phanee_k »

Thanks Ray,

Now it is working.


regards
Phani
Post Reply