How to create a sequential file?

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
JeanPierreHaddad
Participant
Posts: 18
Joined: Mon Nov 25, 2002 3:23 am
Location: Switzerland

How to create a sequential file?

Post by JeanPierreHaddad »

Hello everyone

I am trying to figure out what is the BASIC instruction to create a sequential file that I could read with an OpenSeq and ReadSeq.

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

Post by ArndW »

The OPENSEQ command will create the file if it does not already exist.

OPENSEQ 'filepath' TO FilePtr THEN {if if already exists} ELSE {if it doesn't}
Last edited by ArndW on Thu Mar 10, 2005 9:51 am, edited 1 time in total.
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Actually, Arnd is wrong, you use the CREATE command if the file does not exist. If you have all of your data in a dynamic array, you can use this logic to write the dynamic array to a file:

Code: Select all

      txt = "your data you want to write out"
      ValidOpen = @FALSE
      filename = "/path/to/your/filename"
      OPENSEQ filename TO FILE Then
         ValidOpen = @TRUE
      End Else
         CREATE FILE Then ValidOpen = @TRUE
      End
      If NOT(ValidOpen) Then
         Call DSLogFatal("Could not create the file: ":filename, "OutputFile")
      End

      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
JeanPierreHaddad
Participant
Posts: 18
Joined: Mon Nov 25, 2002 3:23 am
Location: Switzerland

Re: How to create a sequential file?

Post by JeanPierreHaddad »

Thanks very much,

Actually the file is created if it does not exist. But I like the 2nd solution where you write the command to write the file. It allows you to check if your file exists (new or old) and has been opened for usage.

Very neat and well structured,

Thanks a lot.

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

Post by ArndW »

kcbland wrote:Actually, Arnd is wrong, you use the CREATE command ...
Ken,

can we agree that I'm not as right as you; but am not wrong? The OPENSEQ on a file that does not exist will create the file but go down the ELSE branch. It is not as clean as using an explicit CREATE but it works, is documented & I've used it for years (I might change to CREATE, though; I haven't used that before). There is also a difference in syntax twixt using OPENSEQ on a path (which is what I showed) and OPENSEQ using FILE/RECORD syntax.
mhester
Participant
Posts: 622
Joined: Tue Mar 04, 2003 5:26 am
Location: Phoenix, AZ
Contact:

Post by mhester »

Arnd,

Thanks for pointing out that you too were correct. I think it's important for all to understand that there are at least two ways to skin this cat and I have been using both methods since 1984. The complete syntax for your method might look something like -

Code: Select all

      OPENSEQ 'C:\XXXXX.TXT' TO FWORK THEN
         PRINT "FILE OPEN"
      END ELSE
         PRINT "CREATING FILE"
      END

     WEOFSEQ FWORK
     CLOSESEQ FWORK
This will do the same thing as what Ken stated, but with much less code. This solution may not include a lot of the error handling, but this should work just fine.

Thanks for pointing out an alternate method. :-)

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

Post by kcbland »

ArndW wrote:can we agree that I'm not as right as you
Okay, :oops: , I may be wrong, but I do believe there are reasons to use the CREATE statement for the file to be created, I just can't quite remember right now the reasons there is an explicit statement for doing so. If it's an unnecessary extra step, then I stand corrected. :oops:
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
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

With some of the PICK type accounts just opening up an file with OPENSEQ will not create an empty file; but since DataStage always creates PI-type accounts it doesn't make a difference for us - but it is better style to explicitly use the statement created for that purpose.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

I knew there was a reason I did it the way Ken did. I am used to Ultimate flavor.
Mamu Kim
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Technically, OPENSEQ does not create a sequential file. Ever.

Even though the file does not exist, it can be opened. What it actually opened is a communication channel (= file unit) through which the software can interact with the file. OPENSEQ opens the channel, CLOSESEQ closes it. (There's some other hairy stuff around the rotating file pool and whether the file is genuinely open at any point in time, but that's outside the scope of the present discussion.)

If OPENSEQ has been used, and a CLOSESEQ is issued without any operation having written to the file (for example WRITESEQ, WEOFSEQ, WRITEBLK, FLUSH, CREATE), then the file will not come into existence.

If any of these operations has occurred, then the file will come into existence. Buffering may mean that it does not appear until the next sync after CLOSESEQ (unless a non-buffered operation such as WRITESEQF or a write with NOBUF in effect has occurred).

The CREATE statement forces the file to exist, even if nothing is written to it. So, if the design mandates a file to be created, even if zero length, then the CREATE statement is the easiest mechanism to guarantee that.

If there's no requirement to create a file, why bother? Rely on the above rules.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
naren6876
Participant
Posts: 233
Joined: Mon Feb 07, 2005 7:19 pm

Post by naren6876 »

How to write each element in the Array in the next(new) line.
kcbland wrote:Actually, Arnd is wrong, you use the CREATE command if the file does not exist. If you have all of your data in a dynamic array, you can use this logic to write the dynamic array to a file:

Code: Select all

      txt = "your data you want to write out"
      ValidOpen = @FALSE
      filename = "/path/to/your/filename"
      OPENSEQ filename TO FILE Then
         ValidOpen = @TRUE
      End Else
         CREATE FILE Then ValidOpen = @TRUE
      End
      If NOT(ValidOpen) Then
         Call DSLogFatal("Could not create the file: ":filename, "OutputFile")
      End

      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
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Try doing this at the top of the logic to demonstrate how it works:

Code: Select all

txt = ""
txt<-1> = "this is output line 1"
txt<-1> = "this is output line 2"
txt<-1> = "this is output line 3"
txt<-1> = "this is output line 4"
txt<-1> = "this is output line 5"
txt<-1> = "this is output line 6"
You can use the posted logic in a DS Function, and just pass an array of data like above and a filename as arguments to the function. The function will take care of creating the file and writing the contents.

Now, if you want to APPEND to an existing file, you could improve the function to have an extra argument for append versus overwrite. If that argument says append, you just need a conditional statement to seek to the end 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
Post Reply