home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / esqlc / esql / samples.c / example6.sqc < prev    next >
Encoding:
Text File  |  1995-11-21  |  8.7 KB  |  327 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE: example6.sqc
  4. //              
  5. //      Sample Embedded SQL for C application
  6. //
  7. //  FUNCTIONS:
  8. //
  9. //      main() - Main program
  10. //      ErrorHandler - Embedded SQL for C error handler
  11. //
  12. //  COMMENTS:
  13. //
  14. //      Copyright (C) 1992 - 1994 Microsoft Corporation
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17.  
  18. // function prototypes (instead of header file)
  19. void ErrorHandler (void);
  20. void multiple_statements();
  21. void cursor_sp();
  22. void invoke_sp();
  23. void select_into();
  24. void branch();
  25.  
  26. #include <stddef.h>         // standard C run-time header
  27. #include <stdio.h>          // standard C run-time header
  28. #include "gcutil.h"         // utility header
  29.  
  30. // GLOBAL VARIABLES
  31. EXEC SQL BEGIN DECLARE SECTION;
  32. char prep[200];    // prepared statements
  33. EXEC SQL END DECLARE SECTION;
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////
  36. //
  37. //  FUNCTION: main()
  38. //
  39. //      Main application
  40. //
  41. //  PARAMETERS:
  42. //
  43. //      argc - count of command line args
  44. //      argv - array of command line argument strings
  45. //      envp - array of environment strings
  46. //
  47. //  RETURNS: 0 if successful, 1 if error
  48. //
  49. //  COMMENTS:
  50. //
  51. ///////////////////////////////////////////////////////////////////////////////
  52.  
  53. int main (
  54.     int argc,
  55.     char** argv,
  56.     char** envp)
  57. {
  58.     int nRet;           // for return values
  59.  
  60.     EXEC SQL BEGIN DECLARE SECTION;
  61.     // for CONNECT TO
  62.     char szServerDatabase[(SQLID_MAX * 2)+2] = "";
  63.     char szLoginPassword[(SQLID_MAX * 2)+2] = "";
  64.     EXEC SQL END DECLARE SECTION;
  65.  
  66.     // install Embedded SQL for C error handler
  67.     EXEC SQL WHENEVER SQLERROR CALL ErrorHandler();
  68.     // set Embedded SQL for C options
  69.     EXEC SQL SET OPTION LOGINTIME 10;
  70.     EXEC SQL SET OPTION QUERYTIME 100;
  71.  
  72.     // display logo
  73.     printf("Sample Embedded SQL for C application\n");
  74.  
  75.     // get info for CONNECT TO statement
  76.     nRet = GetConnectToInfo(argc, argv,
  77.         szServerDatabase,
  78.         szLoginPassword);
  79.     if (!nRet)
  80.     {
  81.         return (1);
  82.     }
  83.  
  84.     // attempt connection to SQL Server
  85.     EXEC SQL CONNECT TO :szServerDatabase
  86.         USER :szLoginPassword;
  87.     if (SQLCODE == 0)
  88.     {
  89.         printf("Connection to SQL Server established\n");
  90.     }
  91.     else
  92.     {
  93.         // problem connecting to SQL Server
  94.         printf("ERROR: Connection to SQL Server failed\n");
  95.         return (1);
  96.     }
  97.  
  98.     EXEC SQL SET CURSORTYPE CUR_BROWSE;
  99.  
  100.     // Demonstrates multiple T-SQL statements in a SQL batch
  101.     multiple_statements();
  102.  
  103.     // Invoke a stored procedure
  104.     invoke_sp();
  105.  
  106.     // Use a stored procedure in a cursor definition
  107.     cursor_sp();
  108.  
  109.     // Select into a temporary table, then use a singleton select
  110.     // on that table
  111.     select_into();
  112.  
  113.     // Branching in a static SQL batch
  114.     branch();
  115.  
  116.     // disconnect from SQL Server
  117.     EXEC SQL DISCONNECT ALL;
  118.  
  119.     return (0);
  120. }
  121.  
  122. ///////////////////////////////////////////////////////////////////////////////
  123. //
  124. //  FUNCTION: ErrorHandler()
  125. //
  126. //      Called on Embedded SQL for C error, displays fields from SQLCA
  127. //
  128. //  PARAMETERS: none
  129. //
  130. //  RETURNS: none
  131. //
  132. //  COMMENTS:
  133. //
  134. ///////////////////////////////////////////////////////////////////////////////
  135.  
  136. void ErrorHandler (void)
  137. {
  138.     // display error information from SQLCA
  139.     printf("Error Handler called:\n");
  140.     printf("    SQL Code = %li\n", SQLCODE);
  141.     printf("    SQL Server Message %li: '%Fs'\n", SQLERRD1, SQLERRMC);
  142. }
  143.  
  144. /*
  145. ** This procedure demonstrates multiple SQL statements in a
  146. ** SQL batch.  It also uses BEGIN/COMMIT TRANSACTION inside a
  147. ** static SQL batch.
  148. */
  149.  
  150. void multiple_statements()
  151. {
  152.     EXEC SQL BEGIN TRANSACTION
  153.         INSERT INTO authors VALUES('123-45-6789', 'Bear', 'Fozzy',
  154.         '206 555-1212', 'Muppet Show', 'Bellevue', 'WA','98005', 1)
  155.         INSERT INTO authors VALUES('234-56-7890', 'Frog', 'Kermit',
  156.         '206 555-1212', 'Sesame Street', 'Redmond', 'WA','98052', 1)
  157.     COMMIT TRANSACTION
  158.     ;
  159.  
  160.     if (SQLCODE == 0) {
  161.         EXEC SQL DELETE FROM authors
  162.             WHERE au_lname = 'Frog' or au_lname = 'Bear';
  163.  
  164.         if (SQLCODE == 0) {
  165.             printf("The authors have been inserted and deleted.\n");
  166.         }
  167.     }
  168.  
  169.     /* Test SQLCODE again since the DELETE might have failed. */
  170.     if (SQLCODE != 0)
  171.     {
  172.         printf ("ERROR: Delete may have failed\n");
  173.     }
  174. }
  175.  
  176. /* This procedure demonstrates how to invoke stored procedures. */
  177. void invoke_sp()
  178. {
  179.     EXEC SQL BEGIN DECLARE SECTION;
  180.     char  added_type[9];
  181.     EXEC SQL END DECLARE SECTION;
  182.  
  183.     /* Execute the stored procedure 'sp_addtype'. */
  184.     EXEC SQL EXEC master..sp_addtype new_type, int;
  185.  
  186.     if (SQLCODE == 0) {
  187.         EXEC SQL
  188.             SELECT name INTO :added_type FROM master..systypes
  189.             WHERE name = 'new_type';
  190.  
  191.         if (SQLCODE == 0) {
  192.             printf("The type '%s' was added successfully.\n",
  193.                 added_type);
  194.  
  195.             EXEC SQL exec master..sp_droptype new_type;
  196.             if (SQLCODE == 0)
  197.                 printf("%s", "It was then deleted successfully.\n");
  198.         }
  199.     }
  200.  
  201. }
  202.  
  203.  
  204. /*
  205. ** This procedure uses a stored procedure in a cursor definition.
  206. ** Declare the cursor, prepare the stored procedure call, open
  207. ** the cursor, fetch all the rows, then close the cursor.
  208. */
  209.  
  210. void cursor_sp()
  211. {
  212.     EXEC SQL BEGIN DECLARE SECTION;
  213.     char  spid[6];      /* sp_who.spid */
  214.     char  status[12];   /* sp_who.status */
  215.     char  login_id[13]; /* sp_who.login_id */
  216.     char  hostname[11]; /* sp_who.hostname */
  217.     char  blk[6];       /* sp_who.blk */
  218.     char  dbname[11];   /* sp_who.dbname */
  219.     char  cmd[20];      /* sp_who.cmd */
  220.     EXEC SQL END DECLARE SECTION;
  221.  
  222.     /* Declare the cursor for a prepared statement BEFORE the
  223.      * cursor is declared.
  224.      */
  225.     EXEC SQL DECLARE stat_cur CURSOR FOR who;
  226.  
  227.     strcpy(prep, "sp_who");
  228.     EXEC SQL PREPARE who FROM :prep;
  229.  
  230.     /* Check result of prepare. No point continuing if it failed. */
  231.     if (SQLCODE != 0)
  232.     {
  233.         return;
  234.     }
  235.  
  236.         EXEC SQL OPEN stat_cur;
  237.  
  238.     // ignore warnings about truncated character strings
  239.     if (SQLCODE != 0 && SQLCODE != 1)
  240.     {
  241.         return;
  242.     }
  243.  
  244.     printf("The following are the active processes on this SQL Server:\n");
  245.  
  246.     printf("spid   status     loginname     hostname   blk   dbname     cmd\n");
  247.     printf("------ ---------- ------------- ---------- ----- ---------- ----------------\n");
  248.  
  249.     // When no more rows are returned, SQLCODE == 100
  250.     while (SQLCODE == 0) {
  251.         // fetch rows
  252.         EXEC SQL FETCH stat_cur INTO
  253.             :spid, :status, :login_id,
  254.             :hostname, :blk, :dbname, :cmd;
  255.  
  256.           if (SQLCODE == 0) {
  257.             printf("%6s %10s %-13s %10s %5s %-10s %16s\n",
  258.             spid, status, login_id, hostname, blk, dbname, cmd);
  259.         }
  260.  
  261.     }
  262.  
  263.     // SQLCODE should be 100 at this point (no more rows), error if not.
  264.     if (SQLCODE != 100)
  265.     {
  266.         printf("ERROR: SQL Code should be 100;  it is %d\n",SQLCODE);
  267.     }
  268.  
  269.  
  270.     EXEC SQL CLOSE stat_cur;
  271.  
  272.     if (SQLCODE != 0) // Test result of closing the cursor
  273.     {
  274.         printf("ERROR: Closing cursor\n");
  275.     }
  276. }
  277.  
  278.  
  279.  
  280. /*
  281. ** Select into a temporary table, then use a singleton select to
  282. ** prove it worked.  The holdlock keeps the authors table from
  283. ** being modified until we are through.
  284. */
  285.  
  286. void select_into()
  287. {
  288.     strcpy(prep, "SELECT * INTO #temp FROM authors holdlock ");
  289.     strcat(prep, "SELECT * FROM #temp WHERE au_fname = 'Reginald'");
  290.     EXEC SQL EXECUTE IMMEDIATE :prep;
  291.  
  292.     if (SQLCODE != 0) {
  293.         return;
  294.     }
  295.  
  296.     printf( "A temporary copy of authors table has been created ");
  297.     printf( "and one of the authors was successfully selected.\n");
  298. }
  299.  
  300. /* This procedure demonstrates branching in a static SQL batch. */
  301. void branch()
  302. {
  303.     EXEC SQL
  304.         DECLARE @looper int
  305.         SELECT @looper = 1
  306.         while @looper < 11
  307.         begin
  308.             if exists (SELECT * FROM authors WHERE au_lname = 'Frog')
  309.             begin
  310.                 DELETE FROM authors WHERE au_lname = 'Frog'
  311.             end
  312.             else
  313.             begin
  314.                 INSERT INTO authors VALUES('123-45-6789','Frog','Kermit',
  315.                 '206 555-1212','Sesame Street','Redmond','WA','98052',1)
  316.             end
  317.             SELECT @looper = @looper + 1
  318.         end
  319.     ;
  320.  
  321.     if (SQLCODE == 0)
  322.     {
  323.         printf( "A branching and looping statement inside a SQL batch, caused Kermit T. Frog\n");
  324.         printf( "to be inserted and deleted from authors five times.\n");
  325.     }
  326. }
  327.