NeoTekSystems Blog

This Blog is hosted and monitored by NeoTekSystems and its staff.
All programmers and business representatives are welcome
to stop by and participate in discussions about the .NET framework.
Welcome to NeoTekSystems Blog Sign in | Join | Help
in Search

Useful SQL Commands for SQL Server

Last post 06-07-2005, 8:00 PM by jspano. 0 replies.
Sort Posts: Previous Next
  •  06-07-2005, 8:00 PM 3

    Useful SQL Commands for SQL Server

    Here are some useful SQL Server commands that I have been asked several times in the past

    How to find if a table exists in a database

    open a recordset with the following sql statement:

    Code:
    SELECT name FROM sysobjects WHERE xtype = 'U' AND name = 'MyTable'

    If the recordset it at eof then the table doesn't exist if it has a record then the table does exist.

    To get a list of databases on a server execute the following:

    Code:
    SELECT * FROM master..sysdatabases

    To leave out the standard system databases:

    Code:
    SELECT * FROM master..sysdatabases WHERE name NOT IN ('tempbd', 'master', 'model', 'msdb')

    To see if a database table has a column:

    Code:
    SELECT * FROM dbo.syscolumns WHERE id = object_id(N'[dbo].[TableName]') AND name = 'FieldName'

    To see if a stored procedure exists:

    Code:
    SELECT * FROM sysobjects WHERE xtype = 'P' AND name = 'storedprocedurename'

    John Spano
    President
    NeoTekSystems, Inc.
    www.NeoTekSystems.com
    Microsoft MVP, MCSD, MCTS-Windows, MCTS-Web, MCPD-Distributed, MCITP-SQLDev, MCITP-SQLAdmin
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems