home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / esqlc / esql / samples.c / example7.sqc < prev    next >
Encoding:
Text File  |  1995-12-19  |  12.4 KB  |  420 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE: example7.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 read_authors();
  21. void add_author();
  22. void update_confirmation();
  23. void update_remove_author();
  24. void remove_authors();
  25. void rollback_transaction();
  26. void titles_trigger();
  27. void current_users();
  28. void stored_procedure();
  29. void pause();
  30.  
  31. #include <stddef.h>         // standard C run-time header
  32. #include <stdio.h>          // standard C run-time header
  33. #include "gcutil.h"         // utility header
  34.  
  35. // GLOBAL VARIABLES
  36.  
  37. EXEC SQL BEGIN DECLARE SECTION;
  38. char first_name[41];    // authors.au_fname
  39. char last_name[21];     // authors.au_lname
  40. char phone[13];         // authors.phone
  41. char addr[41];          // authors.address
  42. char city[21];          // authors.city   
  43. char state[3];          // authors.state      
  44. char zip[6];            // authors.zip
  45. int  contract;          // authors.contract
  46. char title_id[7];       // titles.title_id
  47. char title[81];         // titles.title
  48. int  ytd_sales;         // titles.ytd_sales
  49. char spid[7];           // sp_who.spid
  50. char id_status[11];     // sp_who.status
  51. char loginame[13];      // sp_who.loginame
  52. char hostname[11];      // sp_who.hostname
  53. char blk[6];            // sp_who.blk
  54. char DBname[11];        // sp_who.dbname
  55. char cmd[17];           // sp_who.cmd
  56. char added_type[81];        // sp_addtype/sp_droptype
  57. char prep[81];          // prepared statements
  58. char server1[81];       // server.database
  59. char server2[81];       // server.database
  60. char user1[81];         // user.password
  61. char user2[81];         // user.password
  62. EXEC SQL END DECLARE SECTION;
  63.  
  64. ///////////////////////////////////////////////////////////////////////////////
  65. //
  66. //  FUNCTION: main()
  67. //
  68. //      Main application
  69. //
  70. //  PARAMETERS:
  71. //
  72. //      argc - count of command line args
  73. //      argv - array of command line argument strings
  74. //      envp - array of environment strings
  75. //
  76. //  RETURNS: 0 if successful, 1 if error
  77. //
  78. //  COMMENTS:
  79. //
  80. ///////////////////////////////////////////////////////////////////////////////
  81.  
  82. int main (
  83.     int argc,
  84.     char** argv,
  85.     char** envp)
  86. {
  87.     int nRet;           // for return values
  88.  
  89.     EXEC SQL BEGIN DECLARE SECTION;
  90.     // for CONNECT TO
  91.     char szServerDatabase[(SQLID_MAX * 2)+2] = "";
  92.     char szLoginPassword[(SQLID_MAX * 2)+2] = "";
  93.     EXEC SQL END DECLARE SECTION;
  94.  
  95.     // install Embedded SQL for C error handler
  96.     EXEC SQL WHENEVER SQLERROR CALL ErrorHandler();
  97.     // set Embedded SQL for C options
  98.     EXEC SQL SET OPTION LOGINTIME 10;
  99.     EXEC SQL SET OPTION QUERYTIME 100;
  100.  
  101.     // display logo
  102.     printf("Sample Embedded SQL for C application\n");
  103.  
  104.     // get info for CONNECT TO statement
  105.     nRet = GetConnectToInfo(argc, argv,
  106.         szServerDatabase,
  107.         szLoginPassword);
  108.     if (!nRet)
  109.     {
  110.         return (1);
  111.     }
  112.  
  113.     // attempt connection to SQL Server
  114.     EXEC SQL CONNECT TO :szServerDatabase
  115.         USER :szLoginPassword;
  116.     if (SQLCODE == 0)
  117.     {
  118.         printf("Connection to SQL Server established\n");
  119.     }
  120.     else
  121.     {
  122.         // problem connecting to SQL Server
  123.         printf("ERROR: Connection to SQL Server failed\n");
  124.         return (1);
  125.     }
  126.  
  127.     // Cursor select with holdlock, begin/commit transaction
  128.     read_authors();
  129.     pause();
  130.  
  131.     // Static insert, multiple SQL statements in a single batch
  132.     add_author();
  133.     read_authors();
  134.     pause();
  135.  
  136.     // Static update and delete, branching in static SQL statement
  137.     update_remove_author();
  138.  
  139.     // Singleton select into
  140.     update_confirmation();
  141.     update_remove_author();
  142.     pause();
  143.     read_authors();
  144.     pause();
  145.  
  146.     // Prepared delete
  147.     remove_authors();
  148.  
  149.     // Static insert, multiple SQL statements in a single batch,
  150.     // begin/rollback transaction
  151.     rollback_transaction();
  152.     read_authors();
  153.     pause();
  154.  
  155.     // Execute immediate, trigger enforcement
  156.     titles_trigger();
  157.  
  158.     // Stored procedure in cursor definition
  159.     current_users();
  160.  
  161.     // Invoke a stored procedure
  162.     stored_procedure();
  163.  
  164.     // disconnect from SQL Server
  165.     EXEC SQL DISCONNECT ALL;
  166.  
  167.     return (0);
  168. }
  169.  
  170. ///////////////////////////////////////////////////////////////////////////////
  171. //
  172. //  FUNCTION: ErrorHandler()
  173. //
  174. //      Called on Embedded SQL for C error, displays fields from SQLCA
  175. //
  176. //  PARAMETERS: none
  177. //
  178. //  RETURNS: none
  179. //
  180. //  COMMENTS:
  181. //
  182. ///////////////////////////////////////////////////////////////////////////////
  183.  
  184. void ErrorHandler (void)
  185. {
  186.     // display error information from SQLCA
  187.     printf("Error Handler called:\n");
  188.     printf("    SQL Code = %li\n", SQLCODE);
  189.     printf("    SQL Server Message %li: '%Fs'\n", SQLERRD1, SQLERRMC);
  190. }
  191.  
  192. void read_authors()
  193. {
  194.     /* Static cursor, non-updatable, holdlock keeps the table locked
  195.      * until the transaction is completed.
  196.      */
  197.     printf( "View all the names from the authors table using a cursor select.\n" );
  198.     EXEC SQL begin transaction;
  199.     EXEC SQL declare stat_cur cursor for select_stat;
  200.  
  201.     strcpy( prep, "select au_fname, au_lname from authors holdlock");
  202.     EXEC SQL prepare select_stat from :prep;
  203.                    
  204.     EXEC SQL SET CURSORTYPE CUR_STANDARD;
  205.  
  206.     EXEC SQL open stat_cur;
  207.  
  208.     /* sqlcode = 100 means that now rows were retrieved */
  209.     while (SQLCODE == 0) {
  210.         /* fetch the next row from the table and display it. */
  211.         EXEC SQL fetch stat_cur into :first_name, :last_name;
  212.  
  213.         if (SQLCODE == 0)
  214.             printf( "%s %s\n", first_name, last_name );
  215.         else if (SQLCODE != 100)
  216.             printf("ERROR: Executing FETCH\n");
  217.     }
  218.     EXEC SQL close stat_cur;
  219.     EXEC SQL commit transaction;
  220. }
  221.  
  222.  
  223.  
  224. /* Static insert, multiple commands in a single batch, commit transaction. */
  225. void add_author()
  226. {
  227.     printf( "Static insert into the authors table of Dybing, ");
  228.     printf( "Fosse, Kothny, and Kermit T. Frog.\n");
  229.     EXEC SQL begin transaction
  230.         insert into authors values("123-45-6789", "Dybing", "Steve",
  231.         "206 454-2030", "One Microsoft Way", "Redmond", "WA",
  232.         "98052", 1)
  233.         insert into authors values("234-56-7890", "Fosse", "Ernie",
  234.         "206 454-2030", "One Microsoft Way", "Redmond", "WA",
  235.         "98052", 1)
  236.         insert into authors values("345-67-8901", "Kothny", "Lilian",
  237.         "206 454-2030", "One Microsoft Way", "Redmond", "WA",
  238.         "98052", 1)
  239.         insert into authors values("456-78-9012", "Frog", "Kermit",
  240.         "206-555-1212", "Sesame Street", "Anytown", "NY",
  241.         "10019", 1)
  242.         commit transaction
  243.     ;
  244. }
  245.  
  246.  
  247. /* Singleton select into program variables. */
  248. void update_confirmation()
  249. {
  250.     printf( "Singleton select to view an inserted author: Dybing\n");
  251.     EXEC SQL
  252.         select au_fname,au_lname
  253.         into :last_name, :first_name
  254.         from authors           
  255.         where au_lname = "Dybing"
  256.     ;
  257.     if (SQLCODE == 0) {
  258.         printf( "%s %s\n", first_name, last_name);
  259.     } else {
  260.         printf("ERROR: Select failed\n");
  261.     }
  262. }
  263.  
  264.  
  265. /* static update and delete, branching in a static sql batch. */
  266. void update_remove_author()
  267. {
  268.     printf( "Branching in a static SQL batch.  If the inserted ");
  269.     printf( "author, Steve Dybing, has not been modified, change his ");
  270.     printf( "first name to Stephen.  If already done, delete him.\n");
  271.     EXEC SQL begin transaction
  272.         if exists (select * from authors
  273.             where au_fname = "Steve" and au_lname = "Dybing")
  274.             update authors
  275.             set au_fname = "Stephen"
  276.             where au_lname = "Dybing"
  277.         else
  278.             delete from authors
  279.                 where au_lname = "Dybing"
  280.         commit transaction
  281.     ;
  282. }
  283.  
  284.  
  285. /* Prepared delete */
  286. void remove_authors()
  287. {
  288.     printf( "Remove the rest of the inserted authors.\n" );
  289.     strcpy( prep, "delete from authors where au_lname = ?");
  290.     EXEC SQL prepare prepared_delete from :prep;
  291.  
  292.     if (SQLCODE == 0) {
  293.         strcpy( last_name, "Kothny");
  294.         EXEC SQL execute prepared_delete using :last_name;
  295.  
  296.         strcpy( last_name, "Fosse");
  297.         EXEC SQL execute prepared_delete using :last_name;
  298.  
  299.         strcpy( last_name, "Frog" );
  300.         EXEC SQL execute prepared_delete using :last_name;
  301.     }
  302. }
  303.  
  304.  
  305. /* Static insert, multiple commands in a single batch, rollback transaction. */
  306. void rollback_transaction()
  307. {
  308.     printf( "Insert four new authors, Gates, Ballmer, Shirley, and ");
  309.     printf( "Gaudette, into the authors table.  Then rollback the ");
  310.     printf( "transaction to remove them.\n");
  311.     EXEC SQL
  312.     BEGIN TRANSACTION rollback_authors
  313.     insert into authors values("111-11-1111", "Gates", "William",
  314.     "206 882-8080", "One Microsoft Way", "Redmond", "WA",
  315.     "98052", 1)
  316.     insert into authors values("222-22-2222", "Ballmer", "Steve",
  317.     "206 882-8080", "One Microsoft Way", "Redmond", "WA",
  318.     "98052", 1)
  319.     insert into authors values("333-33-3333", "Shirley", "John",
  320.     "206 882-8080", "One Microsoft Way", "Redmond", "WA",
  321.     "98052", 1)
  322.     insert into authors values("444-44-4444", "Gaudette", "Frank",
  323.     "206-882-8080", "One Microsoft Way", "Redmond", "WA",
  324.     "98052", 1)
  325.     ROLLBACK TRANSACTION rollback_authors
  326.     ;
  327. }
  328.  
  329.  
  330. /* Execute immediate, foreign key constraint prevents deletion. */
  331. void titles_trigger()
  332. {
  333.     printf( "Try to delete a title, BU1111, from the titles table\n");
  334.     printf( "to show that referntial constraints are enforced.  \n");
  335.     strcpy( prep, "delete titles where title_id = 'BU1111'");
  336.     EXEC SQL execute immediate :prep;
  337.  
  338.     if (SQLCODE == 0) {
  339.         EXEC SQL
  340.         select title_id,  title,  ytd_sales
  341.         into  :title_id, :title, :ytd_sales
  342.         from titles
  343.         where title_id = "BU1111"
  344.         ;
  345.         if (SQLCODE == 0 || SQLCODE == 1) {
  346.             printf( "Deltitle did not allow the deletion of: %s\n", title_id);
  347.             printf( "%s ", title);
  348.             printf( "since it has sold %d copies this year.\n", ytd_sales );
  349.         }   
  350.     }
  351. }
  352.  
  353.  
  354. /* Stored procedure in a cursor definition. */
  355. void current_users()
  356. {
  357.     printf( "View the current users via a stored procedure, sp_who,\n");
  358.     printf( "in a cursor definition.\n");                  
  359.     EXEC SQL declare stat_cur2 cursor for who;
  360.  
  361.     strcpy(prep, "sp_who");
  362.     EXEC SQL prepare who from :prep;
  363.  
  364.     EXEC SQL SET CURSORTYPE CUR_BROWSE;
  365.     EXEC SQL open stat_cur2;
  366.     if (SQLCODE != 0 && SQLCODE != 1) {
  367.         printf("ERROR: Opening cursor\n");
  368.     }        
  369.  
  370.     printf( "spid    status     loginame     hostname   blk   dbname     cmd\n");
  371.     printf( "----    ---------  -----------  ---------  ----  ---------  ----------------\n");
  372.  
  373.     while (SQLCODE == 0) {
  374.         /* Fetch the next row from the table and display it. */
  375.         EXEC SQL 
  376.             fetch stat_cur2 into :spid, :id_status, :loginame,
  377.             :hostname, :blk, :DBname, :cmd  
  378.         ;
  379.         if (SQLCODE == 0) {
  380.             printf(" %s %s %s %s %s %s %s\n",
  381.             spid, id_status, loginame, hostname, blk, DBname, cmd);
  382.         } else if (SQLCODE != 100) {
  383.             printf("ERROR: Executing fetch\n");
  384.         }
  385.     }
  386.     EXEC SQL close stat_cur2;
  387. }
  388.  
  389. /* Use the exec command to execute a stored procedure. */
  390. void stored_procedure()
  391. {
  392.     printf( "Using the stored procedures sp_addtype and sp_droptype\n");
  393.     printf( "to add and drop the datatype: ");
  394.  
  395.     EXEC SQL exec master..sp_addtype demo_new_type, int;
  396.  
  397.     if (SQLCODE == 0) {
  398.         EXEC SQL    
  399.             select name into :added_type
  400.             from master..systypes
  401.             where name = "demo_new_type"
  402.         ;
  403.         if (SQLCODE == 0 || SQLCODE == 1) {
  404.             printf( "%s\n", added_type  );
  405.             EXEC SQL         
  406.                 exec master..sp_droptype demo_new_type
  407.             ;
  408.         }
  409.     }   
  410. }
  411.     
  412. /* Allow the user to set the pace */
  413. void pause()
  414. {
  415.     char junk[80];
  416.  
  417.     printf( "Press ENTER to continue" );
  418.     gets( junk );
  419. }
  420.