Copyright 2024 - BV TallVision IT

In some extreme cases, when you the system simply ignores the optimal index and you want a bit more control over the selection the database is doing for you, you may just have to hint the database server. 

Very uncommon, but it may just be the solution. This will work only if the database management system (DB2, Oracle) supports it and only in the way they support it. A hint can e.g. be used to make the database use a given index, like so: 

SELECT * FROM lips 
   INTO CORRESPONDING FIELDS OF TABLE lta_xlips_other
   FOR ALL ENTRIES IN it_xlips
   WHERE vgbel = it_xlips-vgbel AND
         vbeln <> it_xlips-vbeln
   %_HINTS ORACLE 'INDEX("LIPS" "LIPS~M05")'. 

Again, you are not likely to want to use this. Also note: Oracle does not encourage the use of hints inside or on views (or subqueries) - Oracle performance tuning guide. It is for extreme measures only.

You're running a DB2 database ? Same difference. Here's an example of hints for a DB2 database:

SELECT * FROM lips 
   INTO CORRESPONDING FIELDS OF TABLE lta_xlips_other
   FOR ALL ENTRIES IN it_xlips
   WHERE vgbel = it_xlips-vgbel AND
         vbeln <> it_xlips-vbeln  
   %_HINTS DB2 'INDEX("LIPS" "LIPS~M05")'.

Very similar setup and with a very similar method of chosing the index to be used. Do note however that the DB2 and ORACLE sets of control parameters are very different. Look it up, there is a lot of documentation available.

Taking this furher than hints look up SAP help texts on EXEC SQL, which allows selections via native SQL. SAP example - just to get the idea:

DATA:  connid   TYPE spfli-connid, 
       cityfrom TYPE spfli-cityfrom, 
       cityto   TYPE spfli-cityto. 

EXEC SQL. 
  OPEN dbcur FOR 
    SELECT connid, cityfrom, cityto 
           FROM spfli 
           WHERE mandt  = :sy-mandt AND 
                 carrid = :p_carrid 
ENDEXEC. 

DO. 
  EXEC SQL. 
    FETCH NEXT dbcur INTO :connid, :cityfrom, :cityto 
  ENDEXEC. 
  IF sy-subrc <> 0. 
    EXIT. 
  ELSE. 
    ... 
  ENDIF. 
ENDDO. 

EXEC SQL. 
  CLOSE dbcur 
ENDEXEC.