Welcome to the blog site of SQLServer.in

Archives for Programming SQL – Useful Queries category

Check for existence of a DB

DECLARE @dbname varchar( 128 )
SELECT @dbname = ‘tempdb’ — test
– In SQL6x/70/2000
IF DB_ID( @dbname ) IS NOT NULL
    PRINT ‘DB Exists…’
ELSE
    PRINT ‘DB does not exist…’

– In SQL70/2000
IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.SCHEMATA
           WHERE CATALOG_NAME = @dbname )
    PRINT ‘DB Exists…’
ELSE
    PRINT ‘DB does not exist…’

IF DATABASEPROPERTY( @dbname , ‘IsOffline’ ) IS NOT NULL
    PRINT ‘DB Exists…’
ELSE
    PRINT ‘DB does not exist…’

select convert(varchar(30),object_name(id)) [Table Name], rows from sysindexes
where object_name(id) not like ‘sys%’ and indid = 1
order by object_name(id)
Use DBCC UPDATEUSAGE to update the sysindexes table

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
 

About Author

Krishna is a Senior Database Administrator, having handson experience of 6 years. Areas of expertise includes database engine, Business Intelligence & performance tunning. A regular SQL blogger on his own website, msdn forums, sqlteam.com & believe in sharing his experience with other technology enthusiasts.