home *** CD-ROM | disk | FTP | other *** search
/ Software Collection (I) / TOOLS.iso / b18 / 1.img / EXAMPLE4.BA@ / EXAMPLE4.bin
Encoding:
Text File  |  1992-09-15  |  4.6 KB  |  178 lines

  1.  
  2. 'EXAMPLE4.BAS -- This example illustrates the use of browse-mode functions
  3. 'to determine the source of result columns from ad hoc queries.
  4.  
  5. 'Copyright (c) 1990 by Microsoft Corp.  All rights reserved.
  6.  
  7.  
  8. '$INCLUDE: 'sql.bi'
  9.  
  10. DECLARE SUB SendCmd (SqlConn&)
  11. DECLARE SUB ExamineResults (SqlConn&)
  12.  
  13. MemLeft& = SETMEM(-16384)   'Reserve 16K of heap.
  14. STACK 8192                  'Reserve 8K of stack space.
  15.  
  16. 'Initialize BASIC DB-LIBRARY.
  17. IF SqlInit$ = "" THEN
  18.     PRINT "Can't start BASIC DB-LIBRARY."
  19.     END
  20. END IF
  21.  
  22. 'Get a login record and fill it in with the necessary information.
  23. Login& = SqlLogin&
  24. Result% = SqlSetLUser%(Login&, "loginid")
  25. Result% = SqlSetLPwd%(Login&, "passwd")
  26. Result% = SqlSetLApp%(Login&, "example")
  27.  
  28. 'Get a connection for communicating with SQL Server.
  29. SqlConn& = SqlOpen&(Login&, "server")
  30. IF SqlConn& = 0 THEN END
  31.  
  32. 'Allow the user to type in queries.
  33. DO
  34.     'Send user-generated query.
  35.     CALL SendCmd(SqlConn&)
  36.  
  37.     'Examine results of user's queries.
  38.     CmdCount = 1
  39.     Result% = SqlResults%(SqlConn&)
  40.     DO
  41.         IF Result% = FAIL THEN
  42.             PRINT : PRINT "Command #"; CmdCount; " failed."
  43.         ELSE
  44.             IF SqlRows%(SqlConn&) = FAIL THEN
  45.                 PRINT : PRINT "Command #"; CmdCount; "returned no rows."
  46.             ELSE
  47.  
  48.                 'Print out results of query.
  49.                 PRINT : PRINT "Command #"; CmdCount; ":"
  50.                 CALL ExamineResults(SqlConn&)
  51.  
  52.                 'Throw away all data rows.
  53.                 Result% = SqlCanQuery%(SqlConn&)
  54.  
  55.             END IF
  56.         END IF
  57.         Result% = SqlResults%(SqlConn&)
  58.         CmdCount = CmdCount + 1
  59.     LOOP UNTIL Result% = NO.MORE.RESULTS
  60. LOOP
  61.  
  62.  
  63. 'Sample Output
  64. '
  65. 'Msg No.: 5701  Severity:  2  Level:  0
  66. 'Message: Changed database context from 'master' to 'master'.
  67. '
  68. 'Enter SQL query:
  69. '1> select name, date = crdate from sysobjects
  70. '2> for browse
  71. '3> go
  72. '
  73. 'Command # 1 :
  74. '
  75. 'The following tables were used to generate these query results:
  76. 'sysobjects    (not browsable)
  77. '
  78. 'These are the columns of the target list and their sources:
  79. '  Result column:             Source:                       Browsable?
  80. '  name                       sysobjects.name               no
  81. '  date                       sysobjects.crdate             no
  82. '
  83. 'Enter SQL query:
  84. '1> exit
  85. '
  86.  
  87. 'This routine uses browse-mode functions to find information about the
  88. 'results of a query.
  89. '
  90. SUB ExamineResults (SqlConn&)
  91.     PRINT
  92.     TabCount% = SqlTabCount%(SqlConn&)
  93.     IF TabCount% = 0 THEN
  94.         PRINT "Query must contain 'FOR BROWSE'."
  95.         EXIT SUB
  96.     END IF
  97.     PRINT "The following tables were used to generate these query results:"
  98.     FOR TabNum% = 1 TO TabCount%
  99.         Table$ = SqlTabName$(SqlConn&, TabNum%)
  100.         IF Table$ <> "" THEN
  101.             PRINT Table$, "(";
  102.             IF SqlTabBrowse%(SqlConn&, TabNum%) = FAIL THEN PRINT "not ";
  103.             PRINT "browsable)"
  104.         END IF
  105.     NEXT TabNum%
  106.  
  107.     PRINT
  108.     IF TabCount% THEN
  109.         PRINT "These are the columns of the target list and their sources:"
  110.         ColCount% = SqlNumCols%(SqlConn&)
  111.         PRINT "  Result column:"; TAB(30); "Source:"; TAB(60); "Browsable?"
  112.         FOR ColNum% = 1 TO ColCount%
  113.             TabNum% = 0
  114.             Table$ = SqlTabSource$(SqlConn&, ColNum%, TabNum%)
  115.             Source$ = SqlColSource$(SqlConn&, ColNum%)
  116.             IF Table$ = "" THEN
  117.                 Source$ = "(result of expression)"
  118.             ELSE
  119.                 Source$ = Table$ + "." + Source$
  120.             END IF
  121.             PRINT "  "; SqlColName$(SqlConn&, ColNum%);
  122.             PRINT TAB(30); Source$; TAB(60);
  123.             IF SqlColBrowse%(SqlConn&, ColNum%) = SUCCEED THEN
  124.                 PRINT "yes"
  125.             ELSE
  126.                 PRINT "no"
  127.             END IF
  128.         NEXT ColNum%
  129.     END IF
  130. END SUB
  131.  
  132. 'This routine sends an ad hoc query typed in by the user.
  133. '
  134. SUB SendCmd (SqlConn&)
  135.     PRINT : PRINT "Enter SQL query:"
  136.     Count% = 0
  137.     DO
  138.         Count% = Count% + 1
  139.         PRINT CHR$(Count% + 48); "> ";
  140.         LINE INPUT Query$
  141.  
  142.         IF Query$ = "go" THEN
  143.             Result% = SqlExec%(SqlConn&)
  144.             EXIT DO
  145.         ELSEIF Query$ = "exit" THEN
  146.             PRINT
  147.             CALL SqlExit
  148.             END
  149.         ELSE
  150.             'Keep reading SQL commands into buffer.
  151.             Result% = SqlCmd%(SqlConn&, Query$ + " ")
  152.         END IF
  153.     LOOP
  154. END SUB
  155.  
  156. 'User-defined handlers.  BASIC DB-LIBRARY will call these procedures if
  157. 'there is an error or a message from the server.
  158. '
  159. FUNCTION UserSqlErrorHandler% (SqlConn&, State%, DbErr%, OsErr%, DbErrStr$, OsErrStr$)
  160.     PRINT
  161.     IF SqlDead%(SqlConn&) THEN
  162.         PRINT "Connection to SQL Server failed."
  163.         UserSqlErrorHandler% = INTEXIT
  164.         EXIT FUNCTION
  165.     END IF
  166.     PRINT "State: "; State%; " Error No: "; DbErr%; " OS Error: "; OsErr%
  167.     PRINT "DB-LIBRARY Error: "; DbErrStr$
  168.     IF OsErr% <> DBNOERR THEN PRINT "Operating-System Error: "; OsErrStr$
  169.     UserSqlErrorHandler% = INTCANCEL
  170. END FUNCTION
  171.  
  172. SUB UserSqlMsgHandler (SqlConn&, MsgNo&, Severity%, Level%, Msg$)
  173.     PRINT
  174.     PRINT "Msg No.:"; MsgNo&; " Severity: "; Severity%; " Level: "; Level%
  175.     PRINT "Message: "; Msg$
  176. END SUB
  177.  
  178.