Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all objects of a particular database in SQL Server

I would like to list all objects of a particular database in SQL Server. I created a query as shown below:

select name, type_desc from sys.objects 
WHERE type in ( 'C', 'D', 'F', 'L', 'P', 'PK', 'RF', 'TR', 'UQ', 'V', 'X' ) 
union
select name, type_desc from sys.indexes
order by name

However, this query list all objects of ALL databases rather than a particular database.

My question is: Is there a way to query all objects of just a particular database?

like image 513
titanium Avatar asked Jun 22 '09 18:06

titanium


2 Answers

This is what I use.

  SELECT  o.type_desc AS Object_Type
       ,  s.name AS Schema_Name
       ,  o.name AS Object_Name
    FROM  sys.objects o 
    JOIN  sys.schemas s
      ON  s.schema_id = o.schema_id
   WHERE  o.type NOT IN ('S'  --SYSTEM_TABLE
                        ,'PK' --PRIMARY_KEY_CONSTRAINT
                        ,'D'  --DEFAULT_CONSTRAINT
                        ,'C'  --CHECK_CONSTRAINT
                        ,'F'  --FOREIGN_KEY_CONSTRAINT
                        ,'IT' --INTERNAL_TABLE
                        ,'SQ' --SERVICE_QUEUE
                        ,'TR' --SQL_TRIGGER
                        ,'UQ' --UNIQUE_CONSTRAINT
                        )
ORDER BY  Object_Type
       ,  SCHEMA_NAME
       ,  Object_Name
like image 196
CK1 Avatar answered Oct 16 '22 13:10

CK1


List all procs, views, tables, functions in Sql Server:

SELECT DISTINCT
       o.name AS Object_Name,
       o.type_desc
  FROM sys.sql_modules m
       INNER JOIN
       sys.objects o
         ON m.object_id = o.object_id
 --WHERE  '.' + m.definition + '.' LIKE '%[^a-z]employeeid[^a-z]%'
 order by type_desc, object_name

The comment is if you want to search for a particular (whole) word.

like image 23
live-love Avatar answered Oct 16 '22 14:10

live-love