Skip to content

Instantly share code, notes, and snippets.

@tjwudi
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjwudi/9300811 to your computer and use it in GitHub Desktop.
Save tjwudi/9300811 to your computer and use it in GitHub Desktop.

List all databases

sp_helpdb

Check the current database name

select d.name 
from master..sysdatabases d, master..sysprocesses p 
where d.dbid=p.dbid and p.spid=@@spid

List all user tables

SELECT * FROM sysobjects WHERE type = 'U'

List all columns in a table

SELECT sc.* 
FROM syscolumns sc
INNER JOIN sysobjects so ON sc.id = so.id
WHERE so.name = 'my_table_name'

Thanks to Lukasz Lysik's post on StackOverflow

Drop a database if exists

IF EXISTS (SELECT name FROM sysdatabases WHERE name = 'testdb')
BEGIN
    EXECUTE('DROP DATABASE testdb')
    print '[testdb] dropped'
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment