Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
Hello All,

 

 

 

I came across scenario where in I need to take backup of my ABAP CDS views developed on top of S4 HANA.

 

Some background on where the source of CDS view is stored....

 

CDS view name and its source code is stored in two tables "tabdir"(Contains view name and package name) and "ddddlsrc" (contains View name and source code)

 

There are two approaches that we can use to do the same(Both these approaches accepts Package Name as input and export all the views under the package to the local Dir specified)

 

1st Approach

 

1] JAVA code which will connect to HANA DB where S4 data is stored. form there it will read the source and will put it in txt file on local dir

 

Note:- this approach needs DB level access

 

=>Please find the attached java code

 

=> You need to change the connection property to enter your HANA DB server name and user id and password

 

=> also need to change the query to the correct schema where your SAP S4 data is residing, in my sample its sapsmh.
import java.sql.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.*;

public class HANATest {
public static void main(String[] argv) {
Connection connection = null;
Statement stmt=null;

try {
Class.forName("com.sap.db.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:sap://XXXXXXX:32615/?autocommit=true","username","pass");

if (connection != null) {
System.out.println("Connection to HANA successful!");
stmt = connection.createStatement();
String sql1= "select top 10 ddddlsrc.source,ddddlsrc.ddlname From sapsmh.tadir left outer join sapsmh.ddddlsrc on tadir.obj_name = ddddlsrc.ddlname where tadir.pgmid = 'R3TR' AND tadir.object = 'DDLS' and tadir.devclass='ZBICDS'";
ResultSet resultSet = stmt.executeQuery(sql1);
int test=0;
while (resultSet.next())
{


java.sql.NClob blob = resultSet.getNClob(1);
String table = (resultSet.getString(2)).replaceAll("[//]","");

StringBuffer str = new StringBuffer();
String strng;
BufferedReader bufferRead = new BufferedReader(blob.getCharacterStream());
while ((strng=bufferRead.readLine())!=null)
{ str.append(strng);
str.append("\n");
}
// System.out.println(str.toString());

BufferedWriter bwr = new BufferedWriter(new FileWriter(new File("D:\\CDS_Downlaod\\"+table+".txt")));
bwr.write(str.toString());
bwr.flush();
bwr.close();

test ++;
}

System.out.println("Done!!!");
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("Connection Failed. User/Passwd Error?");
return;
}

}

}

2nd Approach

 

2] ABAP program which will do the same thing by joining the two tables mentioned above and create the files at local disk
*&---------------------------------------------------------------------*
*& Report ZTEST_87
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZCDS_BACKUP NO STANDARD PAGE HEADING.
"structure for DDDDLSCR

TYPES: BEGIN OF ty_ddddlsrc,
ddlname TYPE ddddlsrc-ddlname,
source TYPE ddddlsrc-source,
END OF ty_ddddlsrc,

BEGIN OF ty_dd,
source TYPE ddddlsrc-source,
END OF ty_dd.

DATA: it_ddddlsrc TYPE STANDARD TABLE OF ty_ddddlsrc,
it_dd TYPE STANDARD TABLE OF ty_dd,
wa TYPE ty_ddddlsrc,
wa_dd TYPE ty_dd.


DATA: gv_filename TYPE string,
gv_filetype TYPE char10.
*&---------------------------------------------------------------------*
""""Selection Screen
*&---------------------------------------------------------------------*

PARAMETERS p_dev TYPE tadir-devclass.

DATA : src TYPE ddddlsrc,
dir TYPE tadir.

*&---------------------------------------------------------------------*
""""Start of selection
*&---------------------------------------------------------------------*
Start-of-selection.

select d~ddlname
d~source
From tadir as t join ddddlsrc as d
on t~obj_name = d~ddlname
into TABLE it_ddddlsrc
where t~pgmid = 'R3TR'
AND t~object = 'DDLS'
and t~DEVCLASS = p_dev.

IF sy-subrc = 0.
WRITE ' File downloaded successfully'.
ENDIF.

*&---------------------------------------------------------------------*
""""End of selection
*&---------------------------------------------------------------------*
End-of-selection.

LOOP AT it_ddddlsrc INTO wa.
CONCATENATE 'D:\CDS_Download\' wa-ddlname '.txt' INTO gv_filename.
wa_dd-source = wa-source.
APPEND wa_dd TO it_dd.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = gv_filename
filetype = 'ASC'
write_field_separator = 'X'
TABLES
data_tab = it_dd.
CLEAR gv_filename.
CLEAR it_dd.
ENDLOOP.

 

 

Thank You,

 

Amol Abhyankar
6 Comments
Labels in this area