Datastage Basic WriteSeq

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

nag0143
Premium Member
Premium Member
Posts: 159
Joined: Fri Nov 14, 2003 1:05 am

Datastage Basic WriteSeq

Post by nag0143 »

Hi All
In Basic , How do i write a new record as first record in a file

After going through help, i figured out
openseq file to file.var then
writeseq "my new record" to file.var then
readseq line2 from file then
*do my logic on line2 of the file
---

End
End
CloseSeq
But I am not getting my desired output

output should be
my new record in line1
1234,2345 this record is first record prior to inserting line1

can any one suggest me what i should do..

Thanks.
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

What are you trying to do? Using readseq and writeseq to manipulate data in a file is very tricky. Use readseq to scan a file from beginning to end. Use writeseq to sequentially write to a file from beginning to end.

You cannot move backwards and forwards in a file, manipulating one row, reading another, overwriting a row, etc using these commands.
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
nag0143
Premium Member
Premium Member
Posts: 159
Joined: Fri Nov 14, 2003 1:05 am

Post by nag0143 »

Thanks KC
I am trying to read a file (contains only one record)as an after job routine and check for value in column1 and column2 , if column1 is not equal to column 2 then i am sending an email with this file in the message body,
I am able to do the above ....
But, i want to insert a heading to the file with the column names .How can i insert a heading to a file that i am trying to read .

Hope i am clear

Thanks.
Nag.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

If you want to 'truncate' the sequential file before you start writing to it, use (from memory) WEOFSeq first.
-craig

"You can never have too many knives" -- Logan Nine Fingers
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

You can't 'insert' records into a sequential file. You'd have to write the header record out to a new file and then write the contents of the original file to it after that.
-craig

"You can never have too many knives" -- Logan Nine Fingers
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

The easiest way to "read" a file into an array is to simply "cat" or "type" the file.

Example:

Code: Select all

UNIXcmd = "cat yourfilename"
Call DSExecute("UNIX", UNIXcmd, ScreenOutput, ReturnCode)
The screen output is put into a variable called ScreenOutput. Now it's just an array. Each line in the screen output is a row in the array. So the first line is ScreenOutput<1>. The second line is ScreenOutput<2>, and so forth.

So, to write out a file, simple overwrite it completely. To do this, you'll want to write a routine that you can call whenever you want to output a file. Notice the WEOFSEQ, which truncates an existing file after you've written over it partially. Here's mine I call OutputFile, it's a subroutine you can CALL using InputArg as the filename and @USER0 (global variable) as the array of file contents:

Code: Select all

      ValidOpen = @FALSE
      filename = InputArg
      If filename Then
         OPENSEQ filename TO FILE Then
            ValidOpen = @TRUE
         End Else
            CREATE FILE Then ValidOpen = @TRUE
         End
      End
      If NOT(ValidOpen) Then
         Call DSLogFatal("Could not create the file: ":filename, "OutputFile")
      End

      txt = @USER0
      AM.CNT = DCOUNT(txt, @AM)
      For i=1 to AM.CNT
         WRITESEQ txt<i> TO FILE else
            Call DSLogFatal("Could not update the file: ":filename, "OutputFile")
         End
      Next i
      WEOFSEQ FILE
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
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

chulett wrote:If you want to 'truncate' the sequential file before you start writing to it, use (from memory) WEOFSeq first.
You can use it last as well, it just lops off the remainder of the file.
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
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

That's true. Just start overwriting and then truncate the remainder of it at the end. :wink:
-craig

"You can never have too many knives" -- Logan Nine Fingers
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

You don't need WeofSeq at the end. CloseSeq will write an end of file mark if you have written to the file and you're at the end of the file.

Your problem can be solved fairly easily given that there is only one line in the file.

Code: Select all

OpenSeq .... To hFile
Then
   ReadSeq Line From hFile
   Then
       * Position to beginning of file
       Seek hFile, 0, 0
       Then
           WeofSeq hFile       ; * truncate file
           WriteSeq "Header stuff" To hFile
           Then
                WriteSeq Line To hFile
                Then
                     Call DSLogInfo("File written.  Yay!", "Output File")
                End
           End
       End
   End
End
CloseSeq hFile
I leave it as an exercise to the reader to code the Else and other clauses.
Last edited by ray.wurlod on Tue Nov 09, 2004 3:32 pm, edited 1 time in total.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

ray.wurlod wrote:You don't need WeofSeq at the end. CloseSeq will write an end of file mark if you have written to the file.
Well, ok then... nevermind. Pay no attention to the man behind the curtain.
-craig

"You can never have too many knives" -- Logan Nine Fingers
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

ray.wurlod wrote:You don't need WeofSeq at the end. CloseSeq will write an end of file mark if you have written to the file.

Okay, I haven't tested to back this up, but I believe Ray is mistaken. I have always used WEOFSEQ to insure that a file is truncated. If you open an existing file and then write to it fewer rows than what currently exist in the file, you have to use the WEOFSEQ to remove the remaining bytes from the original file. I'm pretty sure, but....Any other old Universe hacks want to chip in?
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
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

You know, I think you may be right. :? I was under the impression from when I did this kind of thing many moons ago that I had to use WeofSeq to avoid... unpleasant results.

Kind of boggles the mind, but Ray [gasp] might be wrong!
-craig

"You can never have too many knives" -- Logan Nine Fingers
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

It's always possible. No-one's perfect. I'm remembering from Prime INFORMATION, but believe that CLOSESEQ behaves as I described in UniVerse and therefore in DataStage. Maybe will do the test in my copious free time! :lol:
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
mhester
Participant
Posts: 622
Joined: Tue Mar 04, 2003 5:26 am
Location: Phoenix, AZ
Contact:

Post by mhester »

Ken,

You are correct regarding WEOFSEQ and CLOSESEQ. CLOSESEQ does not truncate the file and WRITESEQ certainly does truncate the file. The following is straight from the UV BASIC Reference guide -
Some systems do not support the truncation of disk files. WEOFSEQ is
ignored on these systems, except that WEOFSEQ always works at the
beginning of a file.
So issuing the WEOFSEQ is vital if you want to ensure that the file is truncated.

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

Post by ray.wurlod »

Here's the test program;
0001: OPENSEQ "FREDFILE" TO HFILE
0002: THEN WEOFSEQ HFILE
0003: WRITESEQ "LINE 1" TO HFILE ELSE NULL
0004: WRITESEQ "LINE 2" TO HFILE ELSE NULL
0005: !!! WEOFSEQ HFILE
0006: CLOSESEQ HFILE
0007: STOP
0008: END

I inspected the result with filepeek and with vi. It made no difference whether line 5 was enabled or not. File FREDFILE did not exist prior to execution of the test, so the THEN clause was not executed.

I never claimed that CLOSESEQ would truncate a file, only that it would write an end of file mark at the end of the file.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply