SQL Server Command Line Tools To Manage Your Server
SQL Server Command Line Tools To Manage Your Server
SQLServerCommandLineToolsToManageYourServer
SQLServerCommandLineToolsToManageYourServer
By:GregRobidoux|ReadComments(4)|RelatedTips:More>Monitoring
enteremailaddress
Join
Problem
ThereareseveralusefulcommandsandfunctionsthatareavailableinSQLServer,butnotknowingwhattheyareor
wheretofindmoreinformationaboutthemissometimesaproblem.Havingthesecommandsatyourfingertipsisvery
helpfulwhentryingtosolveaproblemorforjustdoinggeneralanalysisonyourdatabaseinstances.
Solution
FollowingisalistofusefulcommandsthatcanberunusingQueryAnalyzer.Alotofthisinformationcanberetrieved
usingEnterpriseManager,butitisoftenfastertousethesecommandsdirectlyinQueryAnalyzeraswellasmore
detailedinformationisprovided.Eachofthesecommandshasdifferentfunctionalityandknowingthatthecommand
existsandwhattypesofinformationtheyproduceisextremelyhelpfulinmanagingyourSQLServerenvironment
Command
Purpose
sp_helpdb
Thisgivesyouinformationaboutalldatabasesinthe
instanceorspecificinformationaboutonedatabase.
fn_virtualfilestats
Thiscommandwillshowyouthenumberofreadand
writestoadatafile.Usesp_helpdbwiththedatabase
nametoseethelogicalfilenumbersforthedatafilesand
thedatabaseid.
fn_get_sql()
ReturnsthetextoftheSQLstatementforthespecified
SQLhandle.ThisissimilartousingDBCC
INPUTBUFFER,butthiscommandwillshowyou
additionalinformation.Thiscanalsobeembeddedina
processeasierthenusingtheDBCCcommand
MSSQLTipsadditionalinfo
sp_lock
Thiscommandshowsyouallofthelocksthatthesystem
iscurrentlytrackingThisissimilartoinformationyoucan
seeinEnterpriseManager.
sp_help
Thiscommandgivesyouinformationabouttheobjects
withinadatabase.Thecommandwithoutanobjectname
willgiveyoualistofallobjectswithinthedatabase.
sp_who2
Givesyouprocessinformationsimilartowhatyousee
whenusingEnterpriseManager.
sp_helpindex
Givesyouinformationabouttheindexesonatableaswell
asthecolumnsusedfortheindex.
SampleUsage
sp_helpdb
sp_helpdbdatabasename
SELECT*FROM::
fn_virtualfilestats(dabaseid,
logicalfileid)
SELECT*FROM::
fn_virtualfilestats(1,1)
DECLARE@Handle
binary(20)
SELECT@Handle=
sql_handleFROM
sysprocessesWHERE
spid=52SELECT*
FROM
::fn_get_sql(@Handle)
sp_lock
sp_lockspid
sp_lockspid1,spid2
sp_help
sp_helpobjectname
sp_who2
sp_who2spid
sp_helpindexobjectname
MSSQLTipsadditionalinfo
sp_spaceused
Thiscommandshowsyouhowmuchspacehasbeen
allocatedforthedatabase(orifspecifiedanobject)and
howmuchspaceisbeingused.
sp_spaceused
sp_spaceusedobjectname
DBCCCACHESTATS
Displaysinformationabouttheobjectscurrentlyinthe
buffercache.
DBCCCACHESTATS
DBCCCHECKDB
Thiswillchecktheallocationofallpagesinthedatabase
aswellascheckforanyintegrityissues.
DBCCCHECKDB
LatestSQLServerTips
SQLServerMonitoringwithPowershell
andtheSQLServerPlatform:Data
Collection(Part1of3)
TheSQLServerJobManagementYou
WishYouHad
http://www.mssqltips.com/sqlservertip/1029/sqlservercommandlinetoolstomanageyourserver/
1/4
5/14/2015
SQLServerCommandLineToolsToManageYourServer
TroubleshootingCommonSQLServer
BulkInsertErrors
SQLServerAnalysisServiceLevelsof
Granularity
DBCCCHECKTABLE
Thiswillchecktheallocationofallpagesforaspecific
tableorindexaswellascheckforanyintegrityissues.
DBCCDBREINDEX
Thiscommandwillreindexyourtable.Iftheindexnameis
leftoutthenallindexesarerebuilt.Ifthefillfactorissetto
0thenthiswillusetheoriginalfillfactorwhenthetablewas
created.
DBCCCHECKTABLE
(tableName')
MSSQLTipsadditionalinfo
DBCCDBREINDEX
(tablename,indexname,
fillfactor)
DBCCDBREINDEX
(authors,'',70)
DBCCDBREINDEX
('pubs.dbo.authors',
UPKCL_auidind,80)
DBCCPROCCACHE
Thiscommandwillshowyouinformationaboutthe
procedurecacheandhowmuchisbeingused.Spotlight
willalsoshowyouthissameinformation.
DBCCPROCCACHE
DBCCMEMORYSTATUS
DisplayshowtheSQLServerbuffercacheisdividedup,
includingbufferactivity.
DBCCMEMORYSTATUS
DBCCSHOWCONTIG
Thiscommandgivesyouinformationabouthowmuch
spaceisusedforatableandindexes.Information
providedincludesnumberofpagesusedaswellashow
fragmentedthedataisinthedatabase.
MonitorSQLServerTransactionLog
FileFreeSpace
DBCCSHOWCONTIG
DBCCSHOWCONTIG
WITHALL_INDEXES
DBCCSHOWCONTIG
tablename
DBCCSHOW_STATISTICS Thiswillshowhowstatisticsarelaidoutforanindex.You
canseehowdistributedthedataisandwhethertheindex
isreallyagoodcandidateornot.
DBCCSHRINKFILE
DBCC
SHOW_STATISTICS
(tablename,indexname)
Thiswillallowyoutoshrinkoneofthedatabasefiles.This
isequivalenttodoingadatabaseshrink,butyoucan
specifywhatfileandthesizetoshrinkitto.Usethe
sp_helpdbcommandalongwiththedatabasenametosee
theactualfilenamesused.
MSSQLTipsadditionalinfo
DBCCSHRINKFILE
(filename,sizeinMB)
DBCCSHRINKFILE
(DataFile,1000)
DBCCSQLPERF
Thiscommandwillshowyoumuchofthetransactionlogs
arebeingused.
DBCCTRACEON
Thiscommandwillturnonatraceflagtocaptureeventsin
theerrorlog.TraceFlag1204capturesDeadlock
information.
DBCCTRACEOFF
Thiscommandturnsoffatraceflag.
DBCC
SQLPERF(LOGSPACE)
DBCC
TRACEON(traceflag)
DBCC
TRACEOFF(traceflag)
FreeSQLServerLearning
NextSteps
Avoiding,Detecting,
andRepairingSQL
ServerDatabase
Corruption
IsHApittingSQL
DatabaseAdmins
againstVMAdmins?
KeepingthePeace
withSANLess
clusters.
Getfamiliarwiththesecommandlinefunctionsthatyoucanputtouseimmediately
Taketimetolearnmoreaboutthesedifferentoptionsandwhenspecificcommandsshouldbeusedandhowthey
shouldbeused
Lookforfuturetipsoneachofthesecommands
LastUpdate:7/28/2006
Abouttheauthor
GregRobidouxisthePresidentof
EdgewoodSolutionsandaco
founderofMSSQLTips.com.
TheArtofStress
FreeDatabase
Administration
SQLServerAudit
withSQLCompliance
ManagerandSQL
Secure
RelatedResources
MoreSQLServerDBATips...
Viewallmytips
Share
Like
Tweet
Becomeapaidauthor
HowtoRollyour
OwnValue,RegEx
andSoundExPattern
ProfilerinSSIS
http://www.mssqltips.com/sqlservertip/1029/sqlservercommandlinetoolstomanageyourserver/
2/4
5/14/2015
SQLServerCommandLineToolsToManageYourServer
SQLServerSecurity
Essentials
EnterEmailAddress
Join
LearnMore
Postacommentorlettheauthorknowthistiphelpedyou.
Allcommentsarereviewed,sostayonsubjectorwemaydeleteyourcomment.
*Name
Notifyforupdates
*Email
Paragraph
***NOTE***IfyouwanttoincludecodefromSQLServerManagementStudio(SSMS)inyourpost,pleasecopythecodefromSSMSandpaste
thecodeintoatexteditorlikeNotePadbeforecopyingthecodebelowtoremovetheSSMSformatting.
Note:youremailaddressisnotpublished.Requiredfieldsaremarkedwithanasterisk(*)
GetfreeSQLtips:
*EnterCode
SaveComment
Tuesday,January28,20149:47:14PMSaritHod
ReadTheTip
CanyoupleasementionSQLS*PlusthisisagreatfreecommandlinetoolforSQLServer
Saturday,November03,20122:26:33AMRon
ReadTheTip
Thanks.
DoyouhaveupdatesforSS2008R2?
Wednesday,July18,20122:01:25PMJeremyKadlec
ReadTheTip
Sandip,
Iwouldcheckoutthesetips:
http://www.mssqltips.com/sqlservertipcategory/25/maintenance/
http://www.mssqltips.com/sqlservertipcategory/91/maintenanceplans/
HTH.
Thankyou,
JeremyKadlec
Wednesday,July18,20121:51:21AMsandipdeshmukh
ReadTheTip
informationregardingSQLServerCommandLineToolsToManageYourServerisverygoodwithallimportant
information.
thanks,
ifposibblepleasesendmethealldatabasescriptwhichicanuseforDatabasemaintanance.
thanks&Regards,
SandipDeshmukh
http://www.mssqltips.com/sqlservertip/1029/sqlservercommandlinetoolstomanageyourserver/
3/4
5/14/2015
SQLServerCommandLineToolsToManageYourServer
SponsorInformation
Follow
Learning
Resources
Search
Community
MoreInfo
GetFreeSQLTips
DBAs
Tutorials
TipCategories
FirstTimer?
Join
Developers
Webcasts
SearchByTipID
Pictures
About
BIProfessionals
Whitepapers
TopTen
FreeTshirt
Copyright
Google+
Careers
Tools
Authors
Contribute
Privacy
QandA
Events
Disclaimer
Today'sTip
UserGroups
Feedback
AuthoroftheYear
Advertise
RSS
Copyright(c)20062015EdgewoodSolutions,LLCAllrightsreserved
Somenamesandproductslistedaretheregisteredtrademarksoftheirrespectiveowners.
http://www.mssqltips.com/sqlservertip/1029/sqlservercommandlinetoolstomanageyourserver/
4/4