home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / S13237.ZIP / UPSTATSR.C < prev    next >
C/C++ Source or Header  |  1991-10-30  |  4KB  |  147 lines

  1. /* *******************************************************************/
  2. /*                                                                     */
  3. /*  SQL Server 'Update Statistics' utility (DOS)                     */
  4. /*                                                                     */
  5. /*  Dynamicly updates statistics on all tables within the specified  */
  6. /*  database.                                                         */
  7. /*                                                                     */
  8. /*  USAGE:  upstatsr server loginid database [password]                 */
  9. /*                                                                     */
  10. /*  By Damien Lindauer                                                 */
  11. /*  Microsoft Corp.                                                     */
  12. /*                                                                     */
  13. /* *******************************************************************/
  14.  
  15. #define DBMSDOS
  16. #include <stdio.h>
  17. #include <sqlfront.h>
  18. #include <sqldb.h>
  19. #include <conio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #define STDEXIT 0
  24.  
  25. int    err_handler();
  26. int    msg_handler();
  27.  
  28. main(int argc, char *argv[])
  29.  
  30. {
  31.     LOGINREC    *loginS, *loginU;
  32.     DBPROCESS   *dbprocS, *dbprocU;
  33.     DBCHAR        Name[30];
  34.  
  35.     char        Server[30], LoginID[30], Database[30], Password[30];
  36.     char        SelectBuf[50];
  37.  
  38.     int        Count;
  39.  
  40.     dberrhandle(err_handler);
  41.     dbmsghandle(msg_handler);
  42.  
  43.     if ((argc < 4) || (argc > 5))
  44.         {
  45.         printf("\nERROR:  Invalid number of arguments.");
  46.         printf("\nUSAGE:  upstatsr server loginid database [password]\n");
  47.         exit(STDEXIT);
  48.         };
  49.  
  50.     strcpy(Server, argv[1]);
  51.     strcpy(LoginID, argv[2]);
  52.     strcpy(Database, argv[3]);
  53.  
  54.     if (argc == 4)
  55.         strcpy(Password, "");
  56.     else
  57.         strcpy(Password, argv[4]);
  58.  
  59.     printf("\nSQL Server Update Statistics Utility\n");
  60.     printf("--------------------------------------------------------------\n");
  61.     printf("Last Modified:  03/04/91                  By:  Damien Linaduer\n");
  62.     printf("--------------------------------------------------------------\n");
  63.     printf("(Updates statistics for all tables in the specified database.)\n\n");
  64.  
  65.     printf("Attempting to connect to %s...", Server);
  66.  
  67.     loginS = dblogin();
  68.     DBSETLUSER(loginS, LoginID);
  69.     DBSETLPWD(loginS, Password);
  70.     DBSETLAPP(loginS, "UpStats_S");
  71.     dbprocS = dbopen(loginS, Server);
  72.  
  73.     loginU = dblogin();
  74.     DBSETLUSER(loginU, LoginID);
  75.     DBSETLPWD(loginU, Password);
  76.     DBSETLAPP(loginU, "UpStats_U");
  77.     dbprocU = dbopen(loginU, Server);
  78.  
  79.     printf("connection complete.\n");
  80.  
  81.     printf("Retrieving user table names from %s..sysobjects.\n", Database);
  82.  
  83.     dbfcmd(dbprocS, "select name from %s..sysobjects", Database);
  84.     dbfcmd(dbprocS, " where type = 'u'");
  85.  
  86.     printf("Updating statistics on the following tables: \n\n");
  87.  
  88.     dbsqlexec(dbprocS);
  89.     if (dbresults(dbprocS) == SUCCEED)
  90.         {
  91.         Count = 0;
  92.         dbbind(dbprocS, 1, NTBSTRINGBIND, 0, Name);
  93.         while (dbnextrow(dbprocS) != NO_MORE_ROWS)
  94.         {
  95.         printf("%d:  %s..%s  ",++Count, Database, Name);
  96.         dbfcmd(dbprocU, " update statistics %s..%s ", Database, Name);
  97.         dbsqlexec(dbprocU);
  98.         if (dbresults(dbprocU) == SUCCEED)
  99.             printf("(complete)\n");
  100.         else
  101.             printf("(failed)\n");
  102.         };
  103.         };
  104.     dbclose(dbprocS);
  105.     dbclose(dbprocU);
  106.     printf("\nUpdate complete.\n");
  107. }
  108.  
  109.  
  110. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  111. DBPROCESS    *dbproc;
  112. int    severity;
  113. int    dberr;
  114. int    oserr;
  115. char    *dberrstr;
  116. char    *oserrstr;
  117. {
  118.     printf("\nDB-LIBRARY ERROR: %d\n", dberr);
  119.     printf("(%s)", dberrstr);
  120.  
  121.     if (oserr != DBNOERR)
  122.          {
  123.          printf("\nOPERATING-SYSTEM ERROR: %d\n", oserr);
  124.          printf("(%s)",oserrstr);
  125.          };
  126.  
  127.     return(INT_EXIT);
  128. }
  129.  
  130.  
  131. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  132. DBPROCESS    *dbproc;
  133. DBINT    msgno;
  134. int    msgstate;
  135. int    severity;
  136. char    *msgtext;
  137. {
  138.     if (severity > 0)
  139.         {
  140.         printf("\nSQL SERVER MESSAGE:  #%ld, state %d, severity %d\n",
  141.         msgno, msgstate, severity);
  142.         printf("(%s)",msgtext);
  143.  
  144.         return(INT_EXIT);
  145.         }
  146. }
  147.