Append to an existing seq file using WriteSeq command ?

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
mprashant
Participant
Posts: 18
Joined: Thu Apr 29, 2004 1:23 pm

Append to an existing seq file using WriteSeq command ?

Post by mprashant »

I have an existing sequential file that was generated by an earlier DS job - joblog.txt in the below example.
Now In my job Control code for a set of different jobs I need to add a few more lines to this file.
But I seem to be overwrting whats already existing in that file. I have attached my code below

Code: Select all

    FileName = "/fmac/users/c10265/joblog.txt"

      OPENSEQ FileName to FILE
      Then
         Call DSLogInfo ("File": FileName: "OPen","JobControl")

      End
      Else
         Call DSLogInfo ("Creating File","JobControl")
      End
      TXT = "The row count is ":Rowcount
      WRITESEQ TXT to FILE else
         Call DSLogFatal("Could not update the file: ":FileName, "OutputFile" )
      End

      TXT1 = " No of rows that do not match = " :Mismatch
      WRITESEQ TXT1 to FILE else
         Call DSLogFatal("Could not update the file: ":FileName, "OutputFile" )
      End
      
      WEOFSEQ FILE
      CLOSESEQ FILE
xcb
Premium Member
Premium Member
Posts: 66
Joined: Wed Mar 05, 2003 6:03 pm
Location: Brisbane, Australia
Contact:

Post by xcb »

Hi,

I've haven't done any work with PX but if the Basic is the same as in Server than here are my observations.

Once you open your file you need to perform a Seek to move your file pointer to the end of your file in order to append data. Opening and then writing directly will over write any existing data that is at the beginning of the file. Also as you are writing an EOF marker (WEOFSEQ) after you execute your two write statements you are effectively truncating any remaining data in the file.

Also note that the comment "Creating File" in the Else statment of OpenSeq will not actually create the file. If you need to create a file use the statement Create.

The syntax for Seek and Create can be found in the Basic Basic Quide pdf installed with your Datastage client.
Cameron Boog
mhester
Participant
Posts: 622
Joined: Tue Mar 04, 2003 5:26 am
Location: Phoenix, AZ
Contact:

Post by mhester »

I have not used the SEEK statement in a long time but I believe the following syntax would position the file pointer to right before the EOF of an open sequential file.

Code: Select all

SEEK -1, 2
The first argument is the offset in bytes and the second argument is where to offset from which has 3 values -
  • 0 = relative to the beginning of file
    1 = relative to current position
    2 = relative to the end of file
So -1, 2 would offset to just before the end of the file.

Regards,
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

xcb wrote:...
Also note that the comment "Creating File" in the Else statment of OpenSeq will not actually create the file. If you need to create a file use the statement Create...
xcb,

the file will be created using the code mprashant has; there is another current thread dealing with the elegance of using this method, but regardless of stylistic issues it will work as coded.

If you don't believe it, just write a user-function and enter a this 4-liner:

Code: Select all

OPENSEQ '/tmp/IAmANewFile.txt' TO TestFilePtr THEN PRINT 'I already exist' ELSE NULL
WEOFSEQ TestFilePtr
CLOSESEQ TestFilePtr
Ans = ''
mprashant
Participant
Posts: 18
Joined: Thu Apr 29, 2004 1:23 pm

Post by mprashant »

Yes xcb the file does get created. It was code Arnd helped me out with. Also thank you all for your help. I got it to work.
mprashant
Participant
Posts: 18
Joined: Thu Apr 29, 2004 1:23 pm

Post by mprashant »

The code for SEEK is as follows

Code: Select all

SEEK FILE,-1,2
      Then
         Call DSLogInfo ("Appending to File" : FileName,"JobControl")
      End
      Else
         Call DSLogInfo ("Unsuccessful","JobControl")
      End
Thanks xcb and mhester

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

Post by ray.wurlod »

Just for completeness. it's OK to position exactly to EOF - after all, you can't go past it! Therefore I normally use:

Code: Select all

SEEK filevariable, 0, 2
ELSE
   Call DSLogWarn("Error in SEEK", RoutineName)
   GoTo MainExit
END
Don't miss the fact that SEEK requires at least a THEN or an ELSE clause. Or both.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
xcb
Premium Member
Premium Member
Posts: 66
Joined: Wed Mar 05, 2003 6:03 pm
Location: Brisbane, Australia
Contact:

Post by xcb »

ArndW wrote:
xcb wrote:...
Also note that the comment "Creating File" in the Else statment of OpenSeq will not actually create the file. If you need to create a file use the statement Create...
xcb,

the file will be created using the code mprashant has; there is another current thread dealing with the elegance of using this method, but regardless of stylistic issues it will work as coded.

If you don't believe it, just write a user-function and enter a this 4-liner:

Code: Select all

OPENSEQ '/tmp/IAmANewFile.txt' TO TestFilePtr THEN PRINT 'I already exist' ELSE NULL
WEOFSEQ TestFilePtr
CLOSESEQ TestFilePtr
Ans = ''
Hi ArndW,

Yes I read that thread after I posted my reply. I didn't know until now that it worked like that. Its always good to learn something new.
Cameron Boog
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Cameron,

I learned about the CREATE statement in this thread - so all of us have profited :P
Post Reply