4D v16

Using Aliases

Home

 
4D v16
Using Aliases

Using Aliases  


 

 

If an SQL query is too complex and contains long names that make it difficult to read, it is possible to use aliases in order to improve its readability.
Here is the previous example using two aliases: Act for the ACTORS table and Cit for the CITIES table.

  • The initial query in 4D code would be:

     ARRAY LONGINT(aSoldTickets;0)
     ARRAY TEXT(aTitles;0)
     ARRAY TEXT(aDirectors;0)
     C_LONGINT($i;$vInd)
     
     $vInd:=0
     ALL RECORDS([ACTORS])
     For($i;1;Records in selection([ACTORS]))
        $vInd:=$vInd+1
        INSERT IN ARRAY(aTitles;$vInd;1)
        aTitles{$vInd}:=[ACTORS]FirstName+" "+[ACTORS]LastName
        RELATE ONE([ACTORS]Birth_City_ID)
        INSERT IN ARRAY(aDirectors;$vInd;1)
        aDirectors{$vInd}:=[CITIES]City_Name
        NEXT RECORD([ACTORS])
     End for
      ` Initialize the rest of the list box columns in order to display the information
     ARRAY INTEGER(aMovieYear;Size of array(aTitles))
     ARRAY TEXT(aMedias;Size of array(aTitles))
     ARRAY LONGINT(aSoldTickets;Size of array(aTitles))
     ARRAY LONGINT(aNrActors;Size of array(aTitles))
     MULTI SORT ARRAY(aDirectors;>;aTitles;>;aMovieYear;aMedias;aSoldTickets;aNrActors)
  • Using SQL code, the above query becomes:

     ARRAY TEXT(aTitles;0)
     ARRAY TEXT(aDirectors;0)
     
     Begin SQL
        SELECT CONCAT(CONCAT(ACTORS.FirstName,' '),ACTORS.LastName), CITIES.City_Name
        FROM ACTORS AS 'Act', CITIES AS 'Cit'
        WHERE Act.Birth_City_ID=Cit.City_ID
        ORDER BY 2,1
        INTO :aTitles, :aDirectors;
     End SQL
      ` Initialize the rest of the list box columns in order to display the information
     ARRAY INTEGER(aMovieYear;Size of array(aTitles))
     ARRAY TEXT(aMedias;Size of array(aTitles))
     ARRAY LONGINT(aSoldTickets;Size of array(aTitles))
     ARRAY LONGINT(aNrActors;Size of array(aTitles))
  • Using generic SQL commands, the above query becomes:

     ARRAY TEXT(aTitles;0)
     ARRAY TEXT(aDirectors;0)
     C_TEXT($tQueryTxt)
     
     SQL LOGIN(SQL_INTERNAL;"";"")
     $tQueryTxt:=""
     $tQueryTxt:=$tQueryTxt+"SELECT CONCAT(CONCAT(ACTORS.FirstName,' '),ACTORS.LastName), CITIES.City_Name"
     $tQueryTxt:=$tQueryTxt+" FROM ACTORS AS 'Act', CITIES AS 'Cit'"
     $tQueryTxt:=$tQueryTxt+" WHERE Act.Birth_City_ID=Cit.City_ID"
     $tQueryTxt:=$tQueryTxt+" ORDER BY 2,1"
     SQL EXECUTE($tQueryTxt;aTitles;aDirectors)
     SQL LOAD RECORD(SQL all records)
     SQL LOGOUT
      ` Initialize the rest of the list box columns in order to display the information
     ARRAY INTEGER(aMovieYear;Size of array(aTitles))
     ARRAY TEXT(aMedias;Size of array(aTitles))
     ARRAY LONGINT(aSoldTickets;Size of array(aTitles))
     ARRAY LONGINT(aNrActors;Size of array(aTitles))
  • Using the QUERY BY SQL command, we are unable to carry out the query above because it is not possible to pass more than one table as the first parameter.
  • Using the SQL EXECUTE IMMEDIATE command, the query above becomes:

     ARRAY TEXT(aTitles;0)
     ARRAY TEXT(aDirectors;0)
     C_TEXT($tQueryTxt)
     $tQueryTxt:=""
     $tQueryTxt:=$tQueryTxt+"SELECT CONCAT(CONCAT(ACTORS.FirstName,' '),ACTORS.LastName), CITIES.City_Name"
     $tQueryTxt:=$tQueryTxt+" FROM ACTORS AS 'Act', CITIES AS 'Cit'"
     $tQueryTxt:=$tQueryTxt+" WHERE Act.Birth_City_ID=Cit.City_ID"
     $tQueryTxt:=$tQueryTxt+" ORDER BY 2,1"
     $tQueryTxt:=$tQueryTxt+" INTO :aTitles, :aDirectors"
     Begin SQL
        EXECUTE IMMEDIATE :$tQueryTxt;
     End SQL
     ARRAY INTEGER(aMovieYear;Size of array(aTitles))
     ARRAY TEXT(aMedias;Size of array(aTitles))
     ARRAY LONGINT(aSoldTickets;Size of array(aTitles))
     ARRAY LONGINT(aNrActors;Size of array(aTitles))

To test all the above examples, launch the "4D SQL Code Samples" database and go to the main window. You can then choose the query mode and press the Using Aliases button.

 
PROPERTIES 

Product: 4D
Theme: Tutorial

 
HISTORY 

 
ARTICLE USAGE

4D SQL Reference ( 4D v16)