Sunday, December 26, 2010

SQL Server -- Query to see all the tables in a given schema

Information about the tables can be found from sys tables. Here is the stored procedure to find all the tables for a given schema. Input to the procedure is schema name. Example is the sample run of this procedure on Adventureworks database.


CREATE PROC getAllTables(@schemaname VARCHAR(130)) AS
SELECT name AS 'tableName'
FROM sys.tables ST
WHERE SCHEMA_ID(@schemaname) = ST.schema_id

EXEC getAllTables 'HumanResources'



No comments:

Post a Comment