Friday, March 4, 2011

Connect to SQL Server from command prompt – list tables and database

Q. How do I connect to Microsoft SQL Server from command prompt? I just wanted to see list of tables and database.

A. MS- SQL Server is relational database management system.

Its primary query language is Transact-SQL, an implementation of the ANSI/ISO standard Structured Query Language (SQL) used by both Microsoft and Sybase.

There is command line tool available and it is called as sqlcmd. you need to enter the word GO after every command.
On the Start menu click Run. In the Open box type cmd, and then click OK to open a Command Prompt window.
At the command prompt, type sqlcmd.

Press ENTER.

Let us say your username is vivek and password is foo, use:
C:> sqlcmd -U vivek -P foo

Once connected you should see 1> prompt. Type following command to use database called sales:

use sales

To list tables type:



select * from SYSOBJECTS where TYPE = 'U' order by NAME

To List all the databases on the server:


To list fields in a table called foo:

sp_help tablename
sp_help foo

No comments:

Post a Comment