Querying Database Tables and Views

Sometimes your code needs to do more than just query data from the database. For example, you might want to query the structure and objects in the database. For this type of query, a basic approach is to query the available tables and views in your database. For example, to list the tables in the AdventureWorks database, use SQL Server Query Editor to execute the following code:

USE adventureworks
GO
SELECT * FROM sys.tables
GO

Sys.tables is a system database that stores the names of all the tables in the current database. Likewise, if you want a list of all the views in a database, use the sys.views system table by executing the following code:

USE adventureworks
GO
SELECT * FROM sys.views
GO

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish