Important Sql Queries

Introduction: In this article i am going to give some of very useful SQL Queries that are required to us in our day to day programming life.
1. Get current Database Name:  Select DB_NAME()
2. Get Details Of All Databases Present In SQL Server: Select * from sys.databases
3. Rename A Database: Exec sp_renamedb ‘oldDatabaseName’ , ‘newDatabaseName’
             sp_renamedb is the sql server default stored procedure that is used to rename the database and this query is supported in SQL Server 2000 and 2005.
And for later versions of Sql Server we have another query.

ALTER DATABASE ‘oldDatabaseName’ MODIFY NAME = ‘newDatabaseName’

4. Get List Of All Tables Present In Sql Server Along With No of Records:
SELECT [TableName] = O.name, [RowCount] =MAX(I.rows)
FROM sysobjects O, sysindexes I
WHERE O.xtype =’U’AND I.id =OBJECT_ID(O.name)
GROUP BY O.name ORDER BY [RowCount] DESC
5. Get List Of All Tables:  SELECT * FROM sys.Tables
6. Get List Of All Views:  SELECT * FROM sys.Views
7. Get List Of All Stored Procedures:  SELECT * FROM sys.Procedures
8. Get List Of All Objects Of Database:  SELECT * FROM sys.Objects
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s