Help with Embedded SQL

This is a discussion on Help with Embedded SQL within the C Programming forums, part of the General Programming Boards category; Can someone help me understand what is happening in this code. I am confused about what is being done in ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Help with Embedded SQL

    Can someone help me understand what is happening in this code. I am confused about what is being done in the structure INTFC_LKUP which I posted below the code. Thanks.

    Code:
       1042     EXEC SQL SELECT remote_host, ftp_cmd, src_dir, dest_dir,
       1043         xfer_dir, notify_flag
       1044     INTO :intfc_lkup.remote_host, :intfc_lkup.ftp_cmd, :intfc_lkup.src_dir,
       1045         :intfc_lkup.dest_dir, :intfc_lkup.xfer_dir,
       1046         :intfc_lkup.notify_flag
       1047     FROM intfc_lkup
       1048     WHERE intfc_nm = :intfc_lkup.intfc_nm;
    
    
    
    typedef  struct {
        char    intfc_nm[26];
        char    remote_host[26];
        char    ftp_cmd[7];
        char    src_dir[151];
        char    dest_dir[151];
        char    xfer_dir[151];
        char    notify_flag[2];
        char    ftp_username[26];
        char    ftp_password[26];
      } INTFC_LKUP;

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Again, this is DB2 SQL. All the variable names with colons in front of them are referred to as "host variables". The DB2 precompiler will process this C source code and turn the SQL into C code that builds a structure (it's own structure, not yours) with pointers to all these values. At run time, DB2 will then be called, and DB2 will place the values for all the host variables into their appropriate places, which in this case, happens to be in the structure you posted.

    Since it's a SELECT INTO, only one row is expected to be returned to the application.

    Is this a z/OS platform?

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Unix - Aix - Ibm - Ingres

    Thanks. I still am a little confused but looking online for more information. Please correct me if I am wrong. Which I am sure I am wrong. The SELECT grabs records from the db and INTO places these db records into my structure WHERE intfc_nm = :intfc_lkup.intfc_nm;? I am not a programmer yet but I am trying to learn 2 things at once (db and c programming) on my own so thanks for any patience you may have.

    -Carl

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes.

    The table name is "intfc_lkup"

    The columns being selected are: remote_host, ftp_cmd, src_dir, dest_dir, and xfer_dir, notify_flag

    The value from column remote_host is being placed into intfc_lkup.remote_host.
    The value from column ftp_cmd is being placed into intfc_lkup.ftp_cmd, and so on.

    There is only one row expected to be returned, and the column intfc_nm has to equal the value already placed into variable intfc_lkup.intfc_nm.

    SELECT INTO is a special case of SELECT combined with FETCH. Normally, you would do a PREPARE (or OPEN) for the SELECT statement, and then an OPEN CURSOR, followed by however many FETCH CURSOR INTO :var.... statements in a loop, then a CLOSE CURSOR. But, since only one row is being returned (defined by data and application logic), then it can all be combined into a SELECT INTO construct.

    Todd
    Last edited by Dino; 04-10-2008 at 03:09 PM. Reason: typo
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thank you very much

    Your help is greatly appreciated. Thanks for taking time to answer my questions. It at least makes some sense now and I can apply what you just spelled out for me to other code I am running into.

    -Carl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help create a loop for embedded SQL
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 04-24-2008, 07:46 AM
  2. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 04:45 PM
  3. Embedded SQL
    By sarac in forum C Programming
    Replies: 1
    Last Post: 05-04-2006, 10:09 AM
  4. Problem with embedded SQL in C/C++ (ECPG)
    By NeuralClone in forum C Programming
    Replies: 4
    Last Post: 10-21-2005, 06:16 PM
  5. Embedded SQL ... should be quick ...
    By MadGooseXP in forum C Programming
    Replies: 3
    Last Post: 11-05-2002, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21