Regarding COMMON function

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
SharingDS
Participant
Posts: 12
Joined: Fri Apr 28, 2006 8:58 pm

Regarding COMMON function

Post by SharingDS »

Hi Gurus,
I have created one routine using COMMON basic function and it is working fine. When i pass the output of this routine to the same routine then it is giving strange results.
But if i copy this routine (Also changed the memory space name in copy routine) it is working fine even if i passed the result of the first routine to the copied routine.

Can anyone clarify me, Routine created using COMMON basic function, can it be use more than once in the same job or not. If it can, how to use it. (I mean can we use the memory space assigned more than once in the same job )

Kind Regards
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

Pasting the code will give insight on how the variable been used.
Rather than changing the variable name in the copied routine, you can test with the named variable to simulate the similar execution. Because the named variable persist till the existence of the user session.
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
SharingDS
Participant
Posts: 12
Joined: Fri Apr 28, 2006 8:58 pm

Post by SharingDS »

Hi Kumar
Then i need to explain about my rotine and the requirement

In this routine i m comparing first record with the second record based on two keys and processing the first record and so on....
For this, first record i m just capturing in variables and when second record comes (i.e now current record), it compares with the first

record and process the first record and appends the flag according to the comparision to the processing record and captures the second

record in variable (Now It replaces the first record).
And when 3rd record comes (Now it is current record) it compares with the second record key fields and process the 2nd record and appends

the flag according to the comaparision to the processing record and so on.....
When it reaches the last record it will compares the pevious record with before previous record and process the previous record.
Here i no need to process the very last record .(Because it is just duplicate record of last record).


Key1 Key2
Eg:101 AB CD 2135
101 AB AB 3333
102 CD AB 1111
102 CD AB 1111
102 CD AB 1111LastRecord
102 CD AB 1111

Output: Flag
101 AB CD 2135 N
101 AB AB 3333 Y
102 CD AB 1111 N
102 CD AB 1111 N
102 CD AB 1111LastRecordY

Key1 Key2 and FullRec is my arguments

Code: Select all

      Common /IncrRowCompare3/ Initialize,LastRecordInitialize, PrevKey1,PrevPrevKey1, PrevKey2,PrevPrevKey2, PrevFullRec

      Equate RoutineName To 'FindRedundance'
        Ans=@FALSE
*/Process the last record
If LastRecordInitialize=1 Then
  If PrevPrevKey1=PrevKey1 Then
     If PrevPrevKey2=PrevKey2 Then
       Ans=PrevFullRec[1,Len(PrevFullRec)-10] : 'Y' 
     End
     Else
       If PrevPrevKey2<>PrevKey2 Then
       Ans=PrevFullRec[1,Len(PrevFullRec)-10] : 'Y' 
       End
     End
  End
  Else
    If PrevPrevKey1<>PrevKey1 Then
      Ans=PrevFullRec[1,Len(PrevFullRec)-10] : 'Y' 
    End 
  End
End


*/Process the last butone record
Else 
   If Substrings(FullRec,Len(FullRec)-9,10)='LastRecord' Then

     
       If PrevKey1=Key1 Then
          If PrevKey2=Key2 Then
              Ans=PrevFullRec : 'N' 
**********************************************************************************
                 PrevPrevKey1=PrevKey1
                 PrevPrevKey2=PrevKey2
                 LastRecordInitialize=1
                 PrevKey1=Key1
                 PrevKey2=Key2
                 PrevFullRec=FullRec
**********************************************************************************
          End
          Else
            If PrevKey2<>Key2 Then
              Ans=PrevFullRec : 'Y' 
********************************************************************************
                 PrevPrevKey1=PrevKey1
                 PrevPrevKey2=PrevKey2
                 LastRecordInitialize=1
                 PrevKey1=Key1
                 PrevKey2=Key2
                 PrevFullRec=FullRec
********************************************************************************
            End
          End 
       End
       Else
         If PrevKey1<>Key1 Then
            Ans=PrevFullRec : 'Y' 
*********************************************************************************
                 PrevPrevKey1=PrevKey1
                 PrevPrevKey2=PrevKey2
                 LastRecordInitialize=1
                 PrevKey1=Key1
                 PrevKey2=Key2
                 PrevFullRec=FullRec
********************************************************************************* 
         End
       End
   End
   Else
*/Process till before last record
    If Initialize Then

                If PrevKey1=Key1 Then
                     If PrevKey2=Key2 Then 
                        Ans=PrevFullRec : 'N' 
**********************************************************************************************************************
                         PrevKey1=Key1
                         PrevKey2=Key2
                         Initialize=1
***********************************************************************************************************************

                     End
                     Else
                       If PrevKey2<>Key2 Then
                         Ans=PrevFullRec : 'Y' 
***********************************************************************************************************************
                         PrevKey1=Key1
                         PrevKey2=Key2
                         PrevFullRec=FullRec
                         Initialize=1
**********************************************************************************************************************
                       End
                     End
                End
                Else
                  If PrevKey1<>Key1 Then
                    Ans=PrevFullRec : 'Y' 
***********************************************************************************************************************
                         PrevKey1=Key1
                         PrevKey2=Key2
                         PrevFullRec=FullRec
                         Initialize=1
************************************************************************************************************************
                  End
                End
       End
      Else
*/Very First Record
           PrevKey1=Key1
           PrevKey2=Key2
           PrevFullRec=FullRec
           Initialize=1
      End
   End

End

I need to do this comarision to the same record but here Key2 value will change and it need to do the same process.
Here i wanted to pass the output of the routine to the same routine to do this comparision. (If i m calling this rotine second time there will be 2 dummy duplicate records at the end and if i need to call the same routine for 3 times then there will be 3 duplicate dummy records at the end of source file)

Thanks and Regards
Luk
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

Hi Luk,
With a quick glance at your code, I need to clarify few doubts.
Are you just comparing current record and the previous records and identifying the duplicates, based on the Keys and the values?
If so why the level of three records i.e., Current, Prev, PrevPrev?
Why not those declaration be executed in a common step?
Like

Code: Select all

     
       If PrevKey1=Key1 Then
          If PrevKey2=Key2 Then
              Ans=PrevFullRec : 'N'
**********************************************************************************
                 PrevPrevKey1=PrevKey1
                 PrevPrevKey2=PrevKey2
                 LastRecordInitialize=1
                 PrevKey1=Key1
                 PrevKey2=Key2
                 PrevFullRec=FullRec
**********************************************************************************
          End
          Else
            If PrevKey2<>Key2 Then
              Ans=PrevFullRec : 'Y'
********************************************************************************
                 PrevPrevKey1=PrevKey1
                 PrevPrevKey2=PrevKey2
                 LastRecordInitialize=1
                 PrevKey1=Key1
                 PrevKey2=Key2
                 PrevFullRec=FullRec
********************************************************************************
            End
          End
       End
       Else
         If PrevKey1<>Key1 Then
            Ans=PrevFullRec : 'Y'
*********************************************************************************
                 PrevPrevKey1=PrevKey1
                 PrevPrevKey2=PrevKey2
                 LastRecordInitialize=1
                 PrevKey1=Key1
                 PrevKey2=Key2
                 PrevFullRec=FullRec
*********************************************************************************
         End
       End
   End 

Why can't the above be written as below?

Code: Select all

       If PrevKey1=Key1 Then
          If PrevKey2=Key2 Then
              Ans=PrevFullRec : 'N'
          End
          Else
            If PrevKey2<>Key2 Then
              Ans=PrevFullRec : 'Y'
          End
          End
       End
       Else
         If PrevKey1<>Key1 Then
            Ans=PrevFullRec : 'Y'
         End
       End
**********************************************************************************
                 PrevPrevKey1=PrevKey1
                 PrevPrevKey2=PrevKey2
                 LastRecordInitialize=1
                 PrevKey1=Key1
                 PrevKey2=Key2
                 PrevFullRec=FullRec
**********************************************************************************
   End 
And If PrevFullRec is intialized with FullRec, who will concatenation of Flag will affect the variable?
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

The answer to the original question, whether variables declared to be in a COMMON area of memory can be used in the same job, is maybe.

The rule is that they can be used repeatedly in the same process.

For example, the before-stage subroutine, any routines called, and the after-stage subroutine in a Transformer stage are all in the same process and can therefore access variables in the same COMMON area.

However they can not access variables in COMMON declared in the before-job subroutine or in job control code - these are variables owned by a difference process.

Another, inviolable rule is that the shape of a COMMON area may not change. Thus, for example, if one declaration specifies ten scalar variables, then all other declarations referring to the same COMMON area (the one with the same label) must also specify ten scalar variables.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

Hope you got the reply from Ray.
But where are you calling the same routine with in the code?
What is the unexpected result that you got?
By the way, cant the code be simplified as below, my 2 cents.

Code: Select all

     Common /IncrRowCompare3/ Initialize,LastRecordInitialize, PrevKey1,PrevPrevKey1, PrevKey2,PrevPrevKey2, PrevFullRec 

      Equate RoutineName To 'FindRedundance' 
        Ans=@FALSE 
*/Process the last record 
If LastRecordInitialize=1 Then 
  If PrevPrevKey1=PrevKey1 AND PrevPrevKey2=PrevKey2 Then 
       Ans=PrevFullRec[1,Len(PrevFullRec)-10] : 'Y' 
  End 
  Else 
       If PrevPrevKey1<>PrevKey1 Or PrevPrevKey2<>PrevKey2 Or Then 
       Ans=PrevFullRec[1,Len(PrevFullRec)-10] : 'Y' 
       End 
   End 
End 

*/Process the last butone record 
Else 
   If Substrings(FullRec,Len(FullRec)-9,10)='LastRecord' Then 

         If PrevKey1=Key1 AND PrevKey2=Key2 Then 
              Ans=PrevFullRec : 'N' 
       End
       Else
             If PrevKey1<>Key1 Or PrevKey2<>Key2 Then
                 Ans=PrevFullRec : 'Y' 
             End
********************************************************************************** 
                 PrevPrevKey1=PrevKey1 
                 PrevPrevKey2=PrevKey2 
                 LastRecordInitialize=1 
                 PrevKey1=Key1 
                 PrevKey2=Key2 
                 PrevFullRec=FullRec 
********************************************************************************* 
       End 
  End 
End
Else 
     If Initialize Then
     */Process till before last record 
          If PrevKey1=Key1 And PrevKey2=Key2 Then 
              Ans=PrevFullRec : 'N' 
          End
          Else
               If PrevKey1<>Key1 Or PrevKey2<>Key2 Then
                    Ans=PrevFullRec : 'Y'                
               End
          End
**************************************************************************************
                         PrevKey1=Key1 
                         PrevKey2=Key2 
                         PrevFullRec=FullRec 
                         Initialize=1 
**************************************************************************************
     End
End
Else 
*/Very First Record 
           PrevKey1=Key1 
           PrevKey2=Key2 
           PrevFullRec=FullRec 
           Initialize=1 
End 
It can be further simplifed but not without knowing your full requirement. If your are looking for modularization, you can stick on the existing code.
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
SharingDS
Participant
Posts: 12
Joined: Fri Apr 28, 2006 8:58 pm

Post by SharingDS »

Thanks a lot Ray and Kumar for u r answers,

Kumar thanks for simplifying the code.


And Kumar, i just explained with only two keys in the previous example.

Here if u see the example below i have total 4 keys , among those Key1 is the common key across to generate the flags.
Steps :
1), i want to create first flag with Key1 and Key2 and append the flag to the record (Then pass the record to the same routine i.e calling the same routine once again)
2), create the second flag with Key1(Common) and Key3 and append the flag to the record (Then pass the record to the same routine)
3), create the third flag with the Key1(Common) and Key4 and append the flag to the record (and so on...depending on the number of comparision keys)

[Simply, i want to pass the output of the first time called routine to the same routine as input and output of the second time called rotuine as input to the same routine once agian (All this, i want to do in the same transformer stage using stage variables)]

Input :

Key1 Key2 Key3 Key4
101 AB CD 2135 ...........
101 AB AB 3333 ...........
102 CD AB 1111 .........
102 CD AB 1111 ..........
102 CD AB 1111 ..........LastRecord
102 CD AB 1111 ...........
102 CD AB 1111 .............
102 CD AB 1111 ..............


(Dots is other data in the record)
Output:
Flags
101 AB CD 2135 ..... NYY
101 AB AB 3333 ..... YYY
102 CD AB 1111 ... NNN
102 CD AB 1111 ... NNN
102 CD AB 1111..LastRecordYYY

Since i need to generate 3 flags, i have 3 dummy records i.e after the LastRecord.
Here i m calling the same routine more than once in the job and it is giving incorrect results.
I wanted to know wether i can call the same routine (Build using COMMON Basic routine) more than once in the same job ?
If not is there any way i can follow to do it.

Cheers
Luk
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

It would be easy to use Routine to read and loop and write into a file. If its in table, you could even dump it into a file and proceed.
You can count the nubmer of dummy records after the 'LastRecord' and can loop based on the count, if the no of key is dynamic.

Bty, what is the output that you see that does not suits your requirement.

Are you re intialising LastRecordInitialize? Else the second time you call the routine will have the value of LastRecordInitialize=1.
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
Post Reply