join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



Free SQL Server monitoring resources

SQL Server Command Line Tools To Manage Your Server

Written By: Greg Robidoux -- 7/28/2006 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
There are several useful commands and functions that are available in SQL Server, but not knowing what they are or where to find more information about them is sometimes a problem.  Having these commands at your fingertips is very helpful when trying to solve a problem or for just doing general analysis on your database instances.

Solution
Following is a list of useful commands that can be run using Query Analyzer.  A lot of this information can be retrieved using Enterprise Manager, but it is often faster to use these commands directly in Query Analyzer as well as more detailed information is provided.  Each of these commands has different functionality and knowing that the command exists and what types of information they produce is extremely helpful in managing your SQL Server environment

 

Command Purpose Sample Usage
sp_helpdb This gives you information about all databases in the instance or specific information about one database.
  • sp_helpdb
  • sp_helpdb databasename
fn_virtualfilestats This command will show you the number of read and writes to a data file.  Use sp_helpdb with the database name to see the logical file numbers for the data files and the database id.
  • SELECT * FROM :: fn_virtualfilestats(dabaseid, logicalfileid)         
  • SELECT * FROM :: fn_virtualfilestats(1, 1) 
fn_get_sql() Returns the text of the SQL statement for the specified SQL handle.  This is similar to using DBCC INPUTBUFFER, but this command will show you additional information.  This can also be embedded in a process easier then using the DBCC command

MSSQLTips additional info

  • DECLARE @Handle binary(20)
    SELECT @Handle = sql_handle FROM sysprocesses WHERE spid = 52 SELECT * FROM ::fn_get_sql(@Handle)  
sp_lock This command shows you all of the locks that the system is currently tracking  This is similar to information you can see in Enterprise Manager.
  • sp_lock
  • sp_lock spid
  • sp_lock spid1, spid2
sp_help This command gives you information about the objects within a database.  The command without an objectname will give you a list of all objects within the database.
  • sp_help
  • sp_help objectname
sp_who2 Gives you process information similar to what you see when using Enterprise Manager.
  • sp_who2
  • sp_who2 spid
sp_helpindex Gives you information about the indexes on a table as well as the columns used for the index.

MSSQLTips additional info

  • sp_helpindex objectname
sp_spaceused This command shows you how much space has been allocated for the database (or if specified an object) and how much space is being used.
  • sp_spaceused
  • sp_spaceused objectname
DBCC CACHESTATS Displays information about the objects currently in the buffer cache.
  • DBCC CACHESTATS
DBCC CHECKDB This will check the allocation of all pages in the database as well as check for any integrity issues.
  • DBCC CHECKDB
DBCC CHECKTABLE This will check the allocation of all pages for a specific table or index as well as check for any integrity issues.
  • DBCC CHECKTABLE (‘tableName’)
DBCC DBREINDEX This command will reindex your table.  If the indexname is left out then all indexes are rebuilt.  If the fillfactor is set to 0 then this will use the original fillfactor when the table was created.

MSSQLTips additional info

  • DBCC DBREINDEX (tablename, indexname, fillfactor)
  • DBCC DBREINDEX (authors, '', 70)
  • DBCC DBREINDEX ('pubs.dbo.authors', UPKCL_auidind, 80) 
DBCC PROCCACHE This command will show you information about the procedure cache and how much is being used.  Spotlight will also show you this same information.
  • DBCC PROCCACHE
DBCC MEMORYSTATUS Displays how the SQL Server buffer cache is divided up, including buffer activity.
  • DBCC MEMORYSTATUS
DBCC SHOWCONTIG This command gives you information about how much space is used for a table and indexes.  Information provided includes number of pages used as well as how fragmented the data is in the database.
  • DBCC SHOWCONTIG
  • DBCC SHOWCONTIG WITH ALL_INDEXES
  • DBCC SHOWCONTIG tablename
DBCC SHOW_STATISTICS This will show how statistics are laid out for an index.  You can see how distributed the data is and whether the index is really a good candidate or not.
  • DBCC SHOW_STATISTICS (tablename, indexname)
DBCC SHRINKFILE This will allow you to shrink one of the database files. This is equivalent to doing a database shrink, but you can specify what file and the size to shrink it to.  Use the sp_helpdb command along with the database name to see the actual file names used.

MSSQLTips additional info

  • DBCC SHRINKFILE (filename, size in MB)
  • DBCC SHRINKFILE (DataFile, 1000)
 
DBCC SQLPERF This command will show you much of the transaction logs are being used.
  • DBCC SQLPERF(LOGSPACE)
DBCC TRACEON This command will turn on a trace flag to capture events in the error log. Trace Flag 1204 captures Deadlock information.
  • DBCC TRACEON(traceflag)
DBCC TRACEOFF This command turns off a trace flag.
  • DBCC TRACEOFF(traceflag)

Next Steps

  • Get familiar with these command line functions that you can put to use immediately
  • Take time to learn more about these different options and when specific commands should be used and how they should be used
  • Look for future tips on each of these commands
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Free SQL Server monitoring resources

  1. Get your free "Dynamic Management Views Starter Pack", including a SQL Server DMV eBooklet and more than 30 sample scripts.
  2. Download a free trial of SQL Response to monitor your SQL Servers and be alerted to problems in an intuitive interface and by email.
SQL Server DMV Starter Pack
SQL Response logo Red Gate Software - ingeniously simple tools

Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.

Idera - SQL comparison toolset

Idera SQL comparison toolset is a set of products that perform object and data comparison, as well as synchronization. No need to purchase two separate products…get both in a single toolset! The tools are easy-to-use and can save hours of development time and make object and data comparison and synchronization quick and easy.

Download now!



More SQL Server Tools
SQL defrag manager

SQL Backup

SQL Refactor

SQL Data Generator

SQL Prompt


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Quickly and accurately deploy database changes with Red Gate's SQL Compare – the industry standard comparison and deployment tool.

Make the most out of SQL Server - Guaranteed Results - Innovative SQL Server DBAs

Prepare for your next SQL Server interview with CareerQandA.com

Free Whitepaper - The Seven Steps to Successful SQL Server Auditing



Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com