[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

VC6 Compiler and SQL

Hi guys,

I have got this code to run a simple stored procedure (DB2) from the command line. Now - it does not compile in VC6 - but I think thats because it needs to run on a DB2 compiler? Any ideas?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
//#include <sqlca.h>

EXEC SQL INCLUDE SQLCA;
#define  CHECKERR(CE_STR) if (check_error (CE_STR, &sqlca) != 0) return 1;
int main(int argc, char *argv[])
{    
      EXEC SQL BEGIN DECLARE SECTION;
   
            char firstname[13];      
            char userid[9];
            char passwd[19];
            char tradecapid[8];
      
      EXEC SQL END DECLARE SECTION;    
      printf( "Sample C program: STATIC\n" );

      if (argc == 1)
            {      
                  EXEC SQL CONNECT TO AAMC204D;
                  CHECKERR ("CONNECT TO AAMC204D");  
            }  
      else if (argc == 3)
            {
                  strcpy (userid, argv[1]);      
                  strcpy (passwd, argv[2]);
                  EXEC SQL CONNECT TO sample USER :userid USING :passwd;
                  CHECKERR ("CONNECT TO SAMPLE");  
            }  
      else if (argc == 4)
            {
                  strcpy (userid, argv[1]);      
                  strcpy (passwd, argv[2]);
                  strcpy (tradecapid, argv[3]);
                  EXEC SQL CONNECT TO sample USER :userid USING :passwd;
                  CHECKERR ("CONNECT TO SAMPLE");  
            }  
      else
            {
            printf ("\nUSAGE: static [userid passwd]\n\n");      
            return 1;
            }
            
      EXEC SQL INSERT 'TEST STORED PROC' INTO :SPL_INSTR4 FROM SEC_TRD WHERE TRD_CAP_ID = :tradecapid;
      CHECKERR ("SELECT statement");  
      printf( "First name = %s\n", firstname );    
      EXEC SQL CONNECT RESET;
      CHECKERR ("CONNECT RESET");  
      return 0;

}

Errors:

C:\WINNT\Profiles\Adccg19\Desktop\SQLTest.cpp(7) : error C2146: syntax error : missing ';' before identifier 'SQL'
C:\WINNT\Profiles\Adccg19\Desktop\SQLTest.cpp(7) : error C2501: 'EXEC' : missing storage-class or type specifiers
C:\WINNT\Profiles\Adccg19\Desktop\SQLTest.cpp(7) : fatal error C1004: unexpected end of file found

Cheers

Simon
0
simonbennett
Asked:
simonbennett
  • 4
  • 3
1 Solution
 
KangaRooCommented:
The code seems to use something called embedded SQL. Usually this involves a pre-parser which translates the special SQL stuff into valid C code. This system depends on the Database (DB2)
You should probably convert to ODBC, which is independent of the actual DBM, as long as the specific ODBC for the DBM is installed.
0
 
simonbennettAuthor Commented:
Thanks Kangaroo - I did think that It needed something like that. You mention ODBC which I am pretty familiar with - (bear in mind the front end is in VB) - is it possible to call eSQL procs via the ODBC interface? We use DB2CLI at them moment.

Can you think of a better way?

Cheers

Simon
0
 
simonbennettAuthor Commented:
.extra points available for a top anwser...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
KangaRooCommented:
I do it regarly from an iSQL like application which uses ODBC. Pass-through queries from Access also work, well as long as the procedure doesn't 'return' anything that doesn't fit in a 'table'.
Anyway, ODBC has a specific method to call stored procedures using an escape sequence. Details in the manual (sorry :(
0
 
simonbennettAuthor Commented:
Cheers Kangaroo. I am waiting on some info from my US colleagues on the DB2 compiler. I will leave this open to see if anyone else bites...

BTW - bonus points if anyone has a VB example of calling this embedded SQL. Would it just be like

database.execute "StoredProcName", dbfailonerror

or do I have to use the SQL API functions (thats how we talk to DB2 at the mo) to send the Proc name as an SQL statement?

And what about parameters?

I will  give all of my points in the world for a good response, not yet though because we may want to split it.

Cheers

Simon
0
 
SrwCommented:
I've used both.  When using VB, I've use the database.execute method  When using C++, I've used raw ODBC, just send the SQL command with SQLExecDirect.

In either case, just build a string with a complete, valid sql command and everything should work fine.

You know "exec proc_TEST 'string param', 15, '12/01/1999'" or whatever.

You can also do insert/update/delete, and any other db specific commands this way, too.  "use master" for instance.  Obviously, when using DAO/ADO,etc. there are better ways to do inserts, updates and the like.  But, you can also do them all with native sql.

Just remember, if you use db specific syntax, you lose portability.
0
 
simonbennettAuthor Commented:
Ta Kangaroo - sorry about the delay - it's been a 'heavy' old xmas

SIW - will post a Q for you...
0
 
KangaRooCommented:
That is fine. Thanx and a Happy New Year!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now