cancel
Showing results for 
Search instead for 
Did you mean: 

hey guys how to fetch data from db table using subroutine

Former Member
0 Kudos

hi guys,

I am fetching data from db table "mcha". now in the follwing code i get it in "HSDAT1_DATE."

now when i read out_tab with key "HSDAT1". it wont come in out_tab-value.plz tell me what is wrong whether some key condition in out_tab or any thing else.

and what should i write in changing && in script.

thanks in advance

FORM DATE_MGF TABLES IN_TAB STRUCTURE ITCSY
                     OUT_TAB STRUCTURE ITCSY.


 tables :MCHA ,QALS.


  DATA : WERKS1 LIKE QALS-WERK.
  DATA : HSDAT1 LIKE MCHA-HSDAT.



  DATA : DATE(2),
         MONTH(2),
         YEAR(4).

 DATA : HSDAT1_DATE(10).

  READ TABLE IN_TAB WITH KEY 'QALS-WERK'.

  LOOP AT IN_TAB.
    IF SY-TABIX EQ 1.
      WERKS1 = IN_TAB-VALUE.

      IF WERKS1 NE SPACE.
        EXIT.
      ENDIF.
    ENDIF.
  ENDLOOP.


  SELECT SINGLE HSDAT INTO HSDAT1
                      FROM MCHA
                                WHERE  WERKS = WERKS1.




  DATE = HSDAT1+6(2).
  MONTH = HSDAT1+4(2).
  YEAR = HSDAT1+0(4).

  CONCATENATE DATE '.' MONTH '.' YEAR INTO HSDAT1_DATE.



  READ TABLE OUT_TAB WITH KEY 'HSDAT1' .
  IF SY-SUBRC = 0.
    OUT_TAB-VALUE = HSDAT1.
    MODIFY OUT_TAB INDEX SY-TABIX.
  ENDIF.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Did you check your table OUT_TAB, what is the format of date in this table. Please tell me 2-3 values of your internal table. So that we can check whether your read is sucessful.

Thanks,

Archana

Former Member
0 Kudos

hi,

i hv pass out_tab-value = HSDAT1_date.

in above code it was by mistake.

now archana i havent get you.initally mine out_tab is blank..this is the only code i hv writen...

but mine HSDAT1_DATE =00.00.0000. it is stored in this manner only in db table mcha.and i m fetching it on the basis of qals-werk.

Former Member
0 Kudos

Hi,

Let me understanf clearly what you want.

initally mine out_tab is blank

According to you, your table out_tab is blank. And you need to add out_tab-value = HSDAT1_date.

READ TABLE OUT_TAB WITH KEY 'HSDAT1' .
  IF SY-SUBRC = 0.
    OUT_TAB-VALUE = HSDAT1.
    MODIFY OUT_TAB INDEX SY-TABIX.
  ENDIF.

So, if your table is blank, then read statement will fail.

Please let me know if your table out_tab in blank when you are reading the table and if this table contains any field called HSDAT.

Basically i would like to know your internal table out_tab declaration.

Thanks,

Archana

Former Member
0 Kudos

hi archana,

i hv delared

data : HSDAT1 like mcha-hsdat.

wht should i hv to declare more.

i hv declare in same way tht for IN_TAB.

but there i get values by reading qals-werk.

IN_TAB-value = 10000

what should i declare or write plz elaborate me.

i am new to this subroutine structure.

Former Member
0 Kudos

Hi,

If you want to add date field to your internal table out_tab.

Then you refer to below code.

SELECT SINGLE HSDAT INTO HSDAT1
                      FROM MCHA
                                WHERE  WERKS = WERKS1.
 
  DATE = HSDAT1+6(2).
  MONTH = HSDAT1+4(2).
  YEAR = HSDAT1+0(4).
 
  CONCATENATE DATE '.' MONTH '.' YEAR INTO HSDAT1_DATE.

Append HSDAT1_DATE to out_tab.

Thanks,

Archana

Former Member
0 Kudos

hi archana,

now out_tab has 2 fields name and value.in name after appending the value are coming.but its not updating the read statemnt.

thrfor the out_tab-value remains blank.

the loop comes out from sy-subrc.

APPEND HSDAT1_DATE TO OUT_TAB.

  READ TABLE OUT_TAB WITH KEY VALUE = 'HSDAT1'  .
  IF SY-SUBRC = 0.
    OUT_TAB-VALUE = HSDAT1_DATE.
    MODIFY OUT_TAB INDEX SY-TABIX.
  ENDIF.

Former Member
0 Kudos

Hi,

Please refer below code and try this way.

LOOP at OUT_TAB.
    OUT_TAB-VALUE = HSDAT1.
    MODIFY OUT_TAB INDEX SY-TABIX.
ENDLOOP.

Thanks,

Archana

Edited by: Archana Pawar on Feb 11, 2010 7:08 AM

Former Member
0 Kudos

HEY THANKS

ARCHANA..............

PROBLEM SOLVED.

Former Member
0 Kudos

HEY archana,

actually i am passing HSDAT1_DATE to changing parameter.but in output it shows blank.plz chk i hv written

below code in sapscript.

/:PERFORM DATE_MGF IN PROGRAM ZORDER_DATE1
	 	/:USING &QALS-WERK&
	 	/:CHANGING &HSDAT1_DATE&
	 	/:ENDPERFORM

                                           date  : &HSDAT1_DATE&

but in date i m getting blank value i hv chekd in debug.

Answers (2)

Answers (2)

Former Member
0 Kudos

some prob with output printing

Former Member
0 Kudos

Hi,

Is your issue solved?

Thanks,

Archana

Former Member
0 Kudos

HI

its solved..

thank you.

Edited by: k t on Feb 11, 2010 11:24 AM

former_member217544
Active Contributor
0 Kudos

Hi Kt,

Your read statement should be like this:


READ TABLE OUT_TAB WITH KEY VALUE = HSDAT1 .

Change the first read statement also accordingly.

Regards,

Swarna Munukoti.