‎2008 Mar 15 9:52 AM
How to store file in Application layer ?
how to use Open Dataset, Close Dataset
with an example please ?
Point assured
‎2008 Mar 15 10:01 AM
hi check this example..,
&----
*& Report ZTESTPROGRAMFORDOWNLOAD
*&
&----
*&
*&
&----
report ztestprogramfordownload.
tables:zupload.
data:it_pa0002(200).
types: begin of ty_pa0002 ,
pernr like pa0002-pernr,
begda like pa0002-begda,
endda like pa0002-endda,
vorna like pa0002-vorna,
nachn like pa0002-nachn,
end of ty_pa0002.
data:begin of it_error occurs 0 ,
pernr like pa0000-pernr,
end of it_error .
data:it_final type standard table of ty_pa0002 with header line.
data:it_temp type standard table of ty_pa0002 with header line.
parameters:p_file like rlgrap-filename default 'F:\usr\sap\EC6\DVEBMGS00\work\entiraledu1'.
start-of-selection.
open dataset p_file for input in text mode encoding default.
do.
read dataset p_file into it_pa0002.
if sy-subrc = 0.
it_final-pernr = it_pa0002+0(8).
it_final-begda = it_pa0002+18(8).
it_final-endda = it_pa0002+36(8).
it_final-vorna = it_pa0002+54(40).
it_final-nachn = it_pa0002+104(40).
append it_final.
clear it_final.
else.
exit.
endif.
enddo.
if not it_final[] is initial.
sort it_final by pernr begda descending.
delete adjacent duplicates from it_final comparing pernr .
select pernr
begda
endda
vorna
nachn
from pa0002
into table it_temp
for all entries in it_final
where pernr = it_final-pernr.
endif.
loop at it_temp.
zupload-pernr = it_temp-pernr.
zupload-begda = it_temp-begda.
zupload-endda = it_temp-endda.
zupload-vorna = it_temp-vorna.
zupload-nachn = it_temp-nachn.
insert zupload.
clear it_temp.
clear zupload.
endloop.
close dataset p_file.
regards,
venkat.
‎2008 Mar 15 10:18 AM
Hai Monica,
Basic Form of the OPEN DATASET Statement
To open a file on the application server, use the OPEN statement as follows:
Syntax
OPEN DATASET <dsn> [Additions].
This statement opens the file <dsn>. If you do not specify any additions for the mode, the file is opened in binary mode for reading. SY-SUBRC returns 0 if the system opens the file. Otherwise, SY-SUBRC is set to 8.
You enter the filename <dsn> either as a literal or as a field containing the actual name of the file. If you do not specify a path, the system opens the file in the directory in which the R/3 System is running on the application server. To open a file, the user under which the R/3 System is running must have the requisite authorizations at operating system level.
Filenames are platform-specific. You must therefore use file- and pathnames that conform to the rules of the operating system under which your R/3 System is running. However, you can also use logical filenames to ensure that your programs are not operating system-specific. For further information, refer to Using Platform-Independent Filenames.
DATA FNAME(60).
FNAME = '/tmp/myfile'.
OPEN DATASET 'myfile'.
OPEN DATASET FNAME.
This example works as long as your R/3 System is running under UNIX. The program opens the file "myfile" in the directory in which the R/3 System is running, and also opens the file "myfile" in directory "/tmp". However, you would have to change the filename for other operating systems. For example, for OpenVMS, you could write the following:
FNAME = '[TMP]myfile.BIN'
OPEN DATASET 'myfile.BIN'.
Opening a File for Write Access
To open a file for writing, use the FOR OUTPUT addition to the OPEN DATASET statement.
Syntax
OPEN DATASET <dsn> FOR OUTPUT.
If the file does not already exist, it is created automatically. If it does already exist, but is closed, its contents are overwritten. If the file exists and is already open (for read or write access, or for appending), the position is reset to the beginning of the file. If the system can open the file <dsn> successfully, SY-SUBRC is set to 0. If not, it is set to 8.
DATA: MESS(60),
FNAME(10) VALUE '/tmp'.
OPEN DATASET FNAME FOR OUTPUT MESSAGE MESS.
IF SY-SUBRC <> 0.
WRITE: 'SY-SUBRC:', SY-SUBRC,
/ 'System Message:', MESS.
ENDIF.
If the R/3 System is ruining under UNIX, the output looks like this:
The system cannot open the file, since the name you specified is that of a directory.
The following program shows how the system sets the position when you open a file for writing. However, it is better programming style to close files that are already open before you reopen them for a different operation (for further information about closing files, refer to Closing a File).
DATA FNAME(60) VALUE 'myfile'.
DATA NUM TYPE I.
OPEN DATASET FNAME FOR OUTPUT.
DO 10 TIMES.
NUM = NUM + 1.
TRANSFER NUM TO FNAME.
ENDDO.
PERFORM INPUT.
OPEN DATASET FNAME FOR OUTPUT.
NUM = 0.
DO 5 TIMES.
NUM = NUM + 10.
TRANSFER NUM TO FNAME.
ENDDO.
PERFORM INPUT.
CLOSE DATASET FNAME.
OPEN DATASET FNAME FOR OUTPUT.
NUM = 0.
DO 5 TIMES.
NUM = NUM + 20.
TRANSFER NUM TO FNAME.
ENDDO.
PERFORM INPUT.
FORM INPUT.
SKIP.
OPEN DATASET FNAME FOR INPUT.
DO.
READ DATASET FNAME INTO NUM.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
WRITE / NUM.
ENDDO.
ENDFORM.
The output appears as follows:
1
2
3
4
5
6
7
8
9
10
10
20
30
40
50
6
7
8
9
10
20
40
60
80
100
This example performs the following steps using the file "myfile":
It is opened for writing
It is filled with 10 integers (for information about the TRANSFER statement, refer to Writing Data to Files)
It is then opened for reading. The position is reset accordingly to the beginning of the file.
It is read into the field NUM. For information about the READ DATASET statement, refer to Reading Data from Files. The values of NUM are displayed on the screen.
It is reopened for writing The position is reset to the beginning of the file.
It is filled with five integers, which overwrite the previous contents of the file.
It is then reopened for reading. The position is reset to the beginning of the file.
The file is read into the field NUM. The values of NUM are displayed on the screen.
It is closed (for information about the CLOSE DATASET statement, refer to Closing a File).
It is then reopened for writing. The system deletes the existing contents of the file.
It is filled with 5 integers.
It is then opened for reading. The position is reset to the beginning of the file.
The file is read into the field NUM. The values of NUM are displayed on the screen.
Closing a File
To close a file on the application server, use the close statement.
Syntax
CLOSE DATASET <dsn>.
This statement closes the file <dsn>. The naming convention is described in the section Opening a File.
You only need to close a file if you want to delete its contents the next time you open it for write access. For further information and an example, refer to Opening a File for Write Access.
However, to avoid errors, and to make your programs easier to read, you should always close a file before the next OPEN DATASET statement. The CLOSE statement divides your program into logical blocks, and makes them easier to maintain.
DATA FNAME(60) VALUE 'myfile'.
OPEN DATASET FNAME FOR OUTPUT.
.....
CLOSE FNAME.
OPEN DATASET FNAME FOR INPUT.
.....
CLOSE FNAME.
OPEN DATASET FNAME FOR INPUT AT POSITION <pos>.
.....
CLOSE FNAME.
The CLOSE statement is not obligatory in this example, but it does improve the layout of the program.
Regards.
Eshwar.
‎2008 Mar 15 10:19 AM
Hi Monica,
use following code...
{CONCATENATE '/tmp/dms/' ufile INTO dsn.
OPEN DATASET dsn FOR INPUT IN TEXT MODE ENCODING DEFAULT.
CLEAR : it_upd_dt.
DO.
READ DATASET dsn INTO rec.
IF sy-subrc = 0.
wa_upd_dt-msg = rec.
APPEND wa_upd_dt TO it_upd_dt.
CLEAR wa_upd_dt.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET dsn.}
Regards,
Dharmesh vyas
‎2008 Apr 07 7:04 AM
‎2008 Jun 24 8:13 AM
‎2008 Jun 24 8:33 AM
Hiii !!
Just try out this program. and after executing this program
Call transaction AL11. Now you'll get a long list, in that list,
Open a directory with its path as DIR_SAPUSERS .\
Double click it then press ctr F there type this filename which you have mentioned in fname.
&----
*& Report YH1146_FILE2 *
*& *
&----
*& *
*& *
&----
REPORT YH1146_FILE2.
DATA: fname(40),
w_line TYPE i VALUE 1.
DATA:
BEGIN OF fs_flight,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
END OF fs_flight.
DATA:
t_flight LIKE
TABLE OF
fs_flight.
DATA:
t_flight1 LIKE
TABLE OF
fs_flight.
SELECT-OPTIONS:
s_carrid FOR fs_flight-carrid,
s_connid FOR fs_flight-connid.
fname = '.\YH1146test.xls'.
PERFORM get_flight_data.
OPEN DATASET fname FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT t_flight INTO fs_flight.
TRANSFER fs_flight TO fname.
ENDLOOP.
IF sy-subrc EQ 0.
WRITE: 'File Opened On Apps Server'.
ELSE.
WRITE: 'File could not be opened'.
ENDIF.
CLOSE DATASET fname.
&----
*& Form get_flight_data
&----
text
----
--> p1 text
<-- p2 text
----
form get_flight_data .
SELECT carrid
connid
fldate
FROM sflight
INTO TABLE t_flight
WHERE carrid IN s_carrid
AND connid IN s_connid.
endform. " get_flight_data
Regards
Abhijeet Kulshreshtha