Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic SQL select parameters

Former Member
0 Kudos

Until runtime I don't know the table name and column-name on which I have to do a select.

Here is my SQL:

SELECT single <b>w_sum_col_name </b> FROM (w_tbl_name)

INTO ZOH_AUDIT-ZOH_NET1

WHERE REQUID = ZOH_AUDIT-REQUID.

My SQL fails when I have SUM(any_column) in the string w_sum_col_name. However it works if its a just a column name of the table. Failure is when the w_sum_col_name string has an aggregate function.

Error is ..SAPSQL_ILLEGAL_AGGREGATE" CX_SY_DYNAMIC_OSQL_SYNTAX

Any alternatives for my requirement is appreciated.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Try this code.


REPORT zreport NO STANDARD PAGE HEADING MESSAGE-ID zfi
                                        LINE-COUNT 65
                                        LINE-SIZE 120.

PARAMETERS: p_matnr LIKE mara-matnr.

DATA: mytable      LIKE dd02l-tabname,
      fields(50)   TYPE c OCCURS 0 WITH HEADER LINE,
      labst        LIKE mard-labst.

TABLES: mard.

DEFINE select_statement.

  select (&1) into &2
              from (&3)
             where matnr = p_matnr.
  endselect.

END-OF-DEFINITION.

fields = 'SUM( LABST )'.
APPEND fields.

mytable = 'MARD'.

select_statement fields[] labst 'MARD'.

WRITE:/ labst.

7 REPLIES 7

Former Member
0 Kudos

Why are you aggregating when you have a single record?

Regards,

Srikanth

0 Kudos

Sorry, that single in SQL is a typo: Here is the actual one.

SELECT w_sum_col_name FROM (w_tbl_name)

INTO ZOH_AUDIT-ZOH_NET1

WHERE REQUID = ZOH_AUDIT-REQUID.

0 Kudos

Try this, I have never tried but I guess should work

SELECT (w_sum_col_name) FROM (w_tbl_name)

INTO ZOH_AUDIT-ZOH_NET1

WHERE REQUID = ZOH_AUDIT-REQUID.

0 Kudos

unless I wrap the dynamic strings in () they never work.

I already have them in ().

Thanks for answering.

Gopi

0 Kudos

Hi Gopi - well done, now please close this thread - you can mark as 'solved it myself'.

Regards

Neil

Former Member
0 Kudos

Try this code.


REPORT zreport NO STANDARD PAGE HEADING MESSAGE-ID zfi
                                        LINE-COUNT 65
                                        LINE-SIZE 120.

PARAMETERS: p_matnr LIKE mara-matnr.

DATA: mytable      LIKE dd02l-tabname,
      fields(50)   TYPE c OCCURS 0 WITH HEADER LINE,
      labst        LIKE mard-labst.

TABLES: mard.

DEFINE select_statement.

  select (&1) into &2
              from (&3)
             where matnr = p_matnr.
  endselect.

END-OF-DEFINITION.

fields = 'SUM( LABST )'.
APPEND fields.

mytable = 'MARD'.

select_statement fields[] labst 'MARD'.

WRITE:/ labst.

0 Kudos

I resolved this by having string = ( sum(col_name) ) instead of string = (sum(col_name)).

Thank U for all the replies

GOpi