0% found this document useful (0 votes)
17 views

Some Important SQL Queries

This document provides SQL queries to list various database objects in SQL Server, including databases, tables, stored procedures, and user defined functions. It gives queries using system stored procedures and tables to get names of all databases and tables in a database. Stored procedures and user defined functions can be found by querying the sys.objects table filtering on type of 'P' for procedures and type descriptions containing 'FUNCTION'.

Uploaded by

Kavi Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Some Important SQL Queries

This document provides SQL queries to list various database objects in SQL Server, including databases, tables, stored procedures, and user defined functions. It gives queries using system stored procedures and tables to get names of all databases and tables in a database. Stored procedures and user defined functions can be found by querying the sys.objects table filtering on type of 'P' for procedures and type descriptions containing 'FUNCTION'.

Uploaded by

Kavi Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Some Important SQL Queries

1. List all the databases on SQL Servers.



You can do it by following System Stored Procedures and queries:
Collapse | Copy Code
EXEC sp_databases

EXEC sp_helpdb

SELECT name
FROM sys.databases

SELECT name
FROM sys.sysdatabases

2. List all Tables in the database on SQL Servers.

You can find out all tables within a database by the following query:
Collapse | Copy Code
SELECT *
FROM sys.Tables

SELECT * FROM information_schema.tables

SELECT * FROM sysobjects WHERE xtype='U'

Here is a list of other object types you can search for as well:

C: Check constraint
D: Default constraint
F: Foreign Key constraint
L: Log
P: Stored procedure
PK: Primary Key constraint
RF: Replication Filter stored procedure
S: System table
TR: Trigger
U: User table
UQ: Unique constraint
V: View
X: Extended stored procedure

3. List all Stored Procedures in the database on SQL Servers.

You can find out all Stored Procedures within a database by the following query:

Collapse | Copy Code
SELECT name
FROM sys.objects
WHERE type = 'P'
If you want to find it within a period then;

SELECT name
FROM sys.objects
WHERE type = 'P'
AND DATEDIFF(D,create_date, GETDATE()) <5

The above query will find out all Stored Procedures created within 5 days.

4. List all User Defined Functions in the database on SQL Servers.

You can find out all User Defined Functions within a database by the following query:

Collapse | Copy Code
SELECT *
FROM sys.objects
WHERE type_desc LIKE '%FUNCTION%';

You might also like