home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / oleauto / traverse / traverse.sql
Encoding:
Text File  |  1996-04-03  |  1.2 KB  |  40 lines

  1. /** This samples shows how to use the traversal syntax for object hierarchies
  2. *** 
  3. **/
  4.  
  5. declare @hr int, @pobj int
  6. declare @source varchar(30), @desc varchar (200)
  7.  
  8. /* create a new SQLOLE object */
  9. exec @hr = sp_OACreate "SQLOLE.SQLServer", @pobj out
  10. if @hr <> 0
  11.     goto Err
  12.  
  13. /* open a connection to the local database */
  14. exec @hr = sp_OAMethod @pobj, 'Connect("", "sa")'
  15. if @hr <> 0
  16.     goto Err
  17.  
  18. /* get all keys from other tables that reference the Authors table from Pubs database, including all candidate tables */
  19. /* the query result is returned as one string                                                                         */
  20. exec @hr = sp_OAMethod @pobj, 'Databases("pubs").Tables("authors").EnumReferencingKeys(IncludeAllCandidates := FALSE).GetRangeString'
  21. if @hr <> 0
  22.     goto Err
  23.  
  24. /* get the number of columns returned by the above query result */
  25. exec @hr = sp_OAGetProperty @pobj, 'Databases("pubs").Tables("authors").EnumReferencingKeys(IncludeAllCandidates := FALSE).Columns'
  26. if @hr <> 0
  27.     goto Err
  28.  
  29. goto Done
  30.  
  31. Err:    
  32.     Print '## ERROR ##'
  33.     exec sp_OAGetErrorInfo null, @source OUT, @desc OUT
  34.     select hr =  convert (binary(4), @hr), source = @source, description = @desc
  35.     goto Done
  36.  
  37. Done:
  38.     exec sp_OADestroy @pobj
  39.  
  40. go