How to read database and table metadata from MS SQL server with minimum permissions

16,606

I belive INFORMATION_SCHEMA Views could help you.

http://msdn.microsoft.com/en-us/library/ms186778.aspx

Share:
16,606
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to come up with the minimal set of queries that return the databases and tables in a Microsoft SQL Server instance, on as many versions of SQL Server as possible.

    I'm not sure if I should half-answer my own question, but here's what I think I need for 2000 and 2005. Ideally I'd go back further, but I don't have access to older versions:

    Permissions

    2005: a user with VIEW ANY DEFINITION permission

    2000: a user the with public role on all databases to be retrieved

    Databases

    sp_databases
    

    or

    SELECT * FROM sysdatabases
    

    both work on SQL Server 2000 and 2005

    Tables

    2005

    SELECT name FROM <database>.sys.tables
    

    or

    SELECT table_name FROM <database>.information_schema.tables WHERE table_type = 'BASE TABLE'
    

    2000

    SELECT name from <database>.dbo.sysobjects WHERE xtype = 'U'
    
  • Admin
    Admin over 15 years
    You'll see I'm using INFORMATION_SCHEMA in one of my queries above -- however, in 2000, I don't seem to get any results with a user with just the public role, which is why I'm using dbo.sysobjects instead.