onlyforbopi
2/1/2017 - 7:42 PM

ABAP - BASIC + VARIABLES

ABAP - BASIC + VARIABLES

/* Obligatory end of statements with . */
/* If colon notation (,) is used, then its part of
a consecutive line statement */

	WRITE 'Hello'. 
	WRITE 'ABAP'. 
	WRITE 'World'.
	
/* OR */

  WRITE 'Hello',
        'ABAP',
	      'World'.
	      
/* Consecutive statements with use of , */

	WRITE: 'Hello', 
			'ABAP', 
			'World'.

	/* or */
	WRITE: 'Hello', 'ABAP', 'World'. 
/* REFERENCE VARIABLES */

	/* SYNTAX */
	DATA <ref> TYPE REF TO <type> VALUE IS INITIAL. 
	
	EX..
	
	CLASS C1 DEFINITION. 
		PUBLIC SECTION. 
		DATA Bl TYPE I VALUE 1. 
	ENDCLASS. 
	
	DATA: Oref TYPE REF TO C1 , 
	Dref1 LIKE REF TO Oref, 
	Dref2 TYPE REF TO I . 
	CREATE OBJECT Oref. 
	GET REFERENCE OF Oref INTO Dref1. 
	CREATE DATA Dref2. 
	Dref2→* = Dref1→*→Bl.
	
	
	/*In the above code snippet, an object reference Oref and two data reference variables Dref1 and Dref2 are declared.
	Both data reference variables are fully typed and can be dereferenced using the dereferencing operator →* at operand positions.
/* Name of program - Add at top */
/* This will be the name the transaction calls */

	REPORT ZHELLO1. 
	WRITE 'Hello World'.
/* How to insert blank lines  - the SKIP command */
		
		/* skip a single line, works like \n */
		WRITE 'This is the 1st line'. 
		SKIP. 
		WRITE 'This is the 2nd line'.
		
		/* skip multiple lines */
		SKIP <number_of_lines>.
		
		/* skip line up to specified index */
		SKIP TO LINE <line_number>.
/* Error message handling    */

		Message	Type	
		E	Error	The message appears and the application halts at its current point. If the program is running in background mode, the job is canceled and the message is recorded in the job log.
		
		W	Warning	The message appears and the user must press Enter for the application to continue. In background mode, the message is recorded in the job log.
		
		I	Information	A pop-up window opens with the message text and the user must press Enter to continue. In background mode, the message is recorded in the job log.
		
		A	Abend	This message class cancels the transaction that the user is currently using.
		
		S	Success	This provides an informational message at the bottom of the screen. The information displayed is positive in nature and it is just meant for user feedback. The message does not impede the program in any way.
		
		X	Abort	This message aborts the program and generates an ABAP short dump.
		
		
		
		When we create a message for message the ID AB, the MESSAGE command - MESSAGE E011 gives the following output −

		EAB011 This report does not support sub-number summarization.
/* Declaring Basic Variables */

	REPORT YR_SEP_12. 
	DATA text_line TYPE C LENGTH 40. 
	text_line = 'A Chapter on Data Types'. 
	Write text_line. 

	DATA text_string TYPE STRING. 
	text_string = 'A Program in ABAP'. 
	Write / text_string. 

	DATA d_date TYPE D. 
	d_date = SY-DATUM. 
	Write / d_date.
	
	ex..
	DATA <f> TYPE <type> VALUE <val>. 
	
	ex..
	DATA d1(2) TYPE C.  
	DATA d2 LIKE d1.  
	DATA minimum_value TYPE I VALUE 10. 
	
/* SCREEN REPORT + PARAMETERS */

	REPORT ZTest123_01. 
	PARAMETERS: NAME(10) TYPE C, 
	CLASS TYPE I, 
	SCORE TYPE P DECIMALS 2, 
	CONNECT TYPE MARA-MATNR. 

	/*Here, NAME represents a parameter of 10 characters, CLASS specifies a parameter of integer type with the default size in bytes, SCORE represents a packed type parameter with values up to two decimal places, and CONNECT refers to the MARA-MATNF type of ABAP Dictionary*/
/* Comments Line and Inline */

	/* Inline comment - Use " " */
	ex. <commands> "comment"
	
	/* Full line comment */
	ex * commment comment
/* ABAP SYSTEM VARIABLES */

EPORT Z_Test123_01. 

WRITE:/'SY-ABCDE', SY-ABCDE,       
      /'SY-DATUM', SY-DATUM, 
      /'SY-DBSYS', SY-DBSYS, 
      /'SY-HOST ', SY-HOST, 
      /'SY-LANGU', SY-LANGU,
      /'SY-MANDT', SY-MANDT,
      /'SY-OPSYS', SY-OPSYS,
      /'SY-SAPRL', SY-SAPRL,
      /'SY-SYSID', SY-SYSID,
      /'SY-TCODE', SY-TCODE,
      /'SY-UNAME', SY-UNAME,
      /'SY-UZEIT', SY-UZEIT.

	  /*The above code produces the following output −

		SY-ABCDE ABCDEFGHIJKLMNOPQRSTUVWXYZ  
		SY-DATUM 12.09.2015 
		SY-DBSYS ORACLE                   
		SY-HOST sapserver 
		SY-LANGU EN 
		SY-MANDT 800 
		SY-OPSYS Windows NT 
		SY-SAPRL 700 
		SY-SYSID DMO 
		SY-TCODE SE38 
		SY-UNAME SAPUSER 
		SY-UZEIT 14:25:48
/* OPERATORS */

	/* ARITHMETIC : +, -, /, *, MOD */
	REPORT YS_SEP_08. 
	DATA: A TYPE I VALUE 150, 
	B TYPE I VALUE 50, 
	Result TYPE I. 
	Result =  A / B. 
	WRITE / Result.
	
	/* LOGICAL : =, <>, >, <. >=, <=, A1 BETWEEN A2 AND A3, IS INITIAL, IS NOT INITIAL */
	
	
	REPORT YS_SEP_08. 

	DATA: 	A TYPE I VALUE 115,
			B TYPE I VALUE 119.
      
	IF A LT B.
		WRITE: / 'A is less than B'.
    ENDIF
	
	
	REPORT YS_SEP_08. 

	DATA: A TYPE I.
      IF A IS INITIAL.
      WRITE: / 'A is assigned'.
      ENDIF.
	  
	  
	/* STRING OPERATORS, STRING COMPARISON OPERATORS
	
	CO (Contains Only) : Checks whether A is solely composed of the characters in B.
	
	CN (Not Contains ONLY) :Checks whether A contains characters that are not in B.
	
	CA (Contains ANY) : Checks whether A contains at least one character of B.
	
	NA (NOT Contains Any) : Checks whether A does not contain any character of B.
	
	CS (Contains a String)	: Checks whether A contains the character string B.

	NS (NOT Contains a String) : Checks whether A does not contain the character string B.

	CP (Contains a Pattern) : It checks whether A contains the pattern in B.
	
	NP (NOT Contains a Pattern) : It checks whether A does not contain the pattern in B.

	*/

	Ex..
	REPORT YS_SEP_08. 
	DATA: P(10) TYPE C VALUE 'APPLE',
      Q(10) TYPE C VALUE 'CHAIR'.
      IF P CA Q.
	
      WRITE: / 'P contains at least one character of Q'.
      ENDIF.
/* How to suppress zeros and blanks   */

		REPORT Z_Test123_01. 

			DATA: W_NUR(10) TYPE N.
			MOVE 50 TO W_NUR.
			WRITE W_NUR NO-ZERO.     /* NO-ZERO SUPPRESSES LEADING ZEROS */
			
			/* This would normally print 00000000050
			/* NO-ZERO will not print 50
			
			
/* How to insert a full horizontal line ie _____ */

		ULINE.
		
		WRITE 'This is Underlined'.
		ULINE.
/* LITERALS */
	
		/*Text field literals */
		/* IGNORE TRAILING BLANKS */
		REPORT YR_SEP_12. 
		Write 'Tutorials Point'. 
		Write / 'ABAP Tutorial'.
		
		/*String field literals*/
		/* DOES NOT IGNORE TRAILING BLANKS */
		REPORT YR_SEP_12. 
		Write `Tutorials Point `. 
		Write / `ABAP Tutorial `. 
/* CONSTANTS */

		/* SIMPLE CONSTANTS */
		CONSTANTS <f> TYPE <type> VALUE <val>.
		
		EX..
		REPORT YR_SEP_12. 
		CONSTANTS PQR TYPE P DECIMALS 4 VALUE '1.2356'. 
		Write: / 'The value of PQR is:', PQR.

		/* COMPLEX CONSTANTS */
		BEGIN OF EMPLOYEE,  
		Name(25) TYPE C VALUE 'Management Team',  
		Organization(40) TYPE C VALUE 'Tutorials Point Ltd',  
		Place(10) TYPE C VALUE 'India',  
		END OF EMPLOYEE.

		/* CONSTANT REFERENCES */
		CONSTANTS null_pointer TYPE REF TO object VALUE IS INITIAL.
1.  abapname.abap             :  Name the program
2.  simpleconcecutline.abap   :  Simple and consecutive abap statements
3.  comments.abap             :  Insert comments
4.  suppressblankszeros.abap  :  Suppress leading blanks or zeros
5.  insertblankline.abap      :  Inserts a blank line
6.  inserthorizontal.abap     :  Inserts a horizontal line
7.  errorhandling.abap        :  Error message handling
8.  declarevar.abap           :  Declare basic variables
9.  declareparameter.abap     :  Declare screen parameter
10. referencevar.abap         :  How to reference variables
11. abapsystemvar.abap        :  How to call on preset system variables
12. abapconstants.abap        :  How to declare constants in abap
13. abapliteralstring.abap    :  How to declare / call string literals
14. abapoperators.abap        :  Mathematic / String / Logical operators