Skip to content
iTecNote
  • Python
  • Javascript
  • PHP
  • Java
  • Android
  • iOS
  • jQuery
  • MySQL

Sql-server – How to list all objects of a particular database in SQL Server

metadatasql-servertsql

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?

Best Solution

Which database are you running this in? When I run it in a particular database, I don't get anything outside that database.

Related Solutions

Sql – Add a column with a default value to an existing table in SQL Server

Syntax:

ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
WITH VALUES

Example:

ALTER TABLE SomeTable
        ADD SomeCol Bit NULL --Or NOT NULL.
 CONSTRAINT D_SomeTable_SomeCol --When Omitted a Default-Constraint Name is autogenerated.
    DEFAULT (0)--Optional Default-Constraint.
WITH VALUES --Add if Column is Nullable and you want the Default Value for Existing Records.

Notes:

Optional Constraint Name:
If you leave out CONSTRAINT D_SomeTable_SomeCol then SQL Server will autogenerate
    a Default-Contraint with a funny Name like: DF__SomeTa__SomeC__4FB7FEF6

Optional With-Values Statement:
The WITH VALUES is only needed when your Column is Nullable
    and you want the Default Value used for Existing Records.
If your Column is NOT NULL, then it will automatically use the Default Value
    for all Existing Records, whether you specify WITH VALUES or not.

How Inserts work with a Default-Constraint:
If you insert a Record into SomeTable and do not Specify SomeCol's value, then it will Default to 0.
If you insert a Record and Specify SomeCol's value as NULL (and your column allows nulls),
    then the Default-Constraint will not be used and NULL will be inserted as the Value.

Notes were based on everyone's great feedback below.
Special Thanks to:
    @Yatrix, @WalterStabosz, @YahooSerious, and @StackMan for their Comments.

Sql – How to return only the Date from a SQL Server DateTime datatype
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))

for example

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

gives me

2008-09-22 00:00:00.000

Pros:

  • No varchar<->datetime conversions required
  • No need to think about locale
Related Question
  • Sql-server – How to check if a column exists in a SQL Server table
  • Sql-server – LEFT JOIN vs. LEFT OUTER JOIN in SQL Server
  • Sql – How to do an UPDATE statement with JOIN in SQL Server
  • Sql – How to Join to first row
  • Sql – How to UPDATE from a SELECT in SQL Server
  • Sql – Get all table names of a particular database by SQL query
  • Sql – Find all tables containing column with specified name – MS SQL Server
  • Sql-server – Get size of all tables in database