忘记SQLServer数据sa密码怎么办,一条命令行搞定

今天帮客户处理SQLServer数据库,发现没有数据库管理系统,也不知道是什么版本的数据,客户也不记得sa密码,所以想起来命令行修改数据库密码的事,为了方便以后使用和有用的网友,特总结本文。

今天帮客户处理SQLServer数据库,发现没有数据库管理系统,也不知道是什么版本的数据,客户也不记得sa密码,所以想起来命令行修改数据库密码的事,为了方便以后使用和有用的网友,特总结本文。

osql
osql

先说一下管理SQLServer的两个命令行工具OSql.exe与SqlCmd.exe

osql是SqlServer 2000的命令行管理工具,而新的sqlcmd取代了Sql Server 2000的osql,以下是两个命令的帮助信息:

以下是osql和sqlcmd的帮助:
C:/Documents and Settings/...>osql/?
Microsoft (R) SQL Server Command Line Tool
Version 10.0.1600.22 NT INTEL X86
Copyright (c) Microsoft Corporation. All rights reserved.

Note: osql does not support all features of SQL Server 2008.
Use sqlcmd instead. See SQL Server Books Online for details.

usage: osql              [-U login id]          [-P password]
[-S server]            [-H hostname]          [-E trusted connection]
[-d use database name] [-l login timeout]     [-t query timeout]
[-h headers]           [-s colseparator]      [-w columnwidth]
[-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
[-L list servers]      [-c cmdend]            [-D ODBC DSN name]
[-q "cmdline query"]   [-Q "cmdline query" and exit]
[-n remove numbering] [-m errorlevel]
[-r msgs to stderr]    [-V severitylevel]
[-i inputfile]         [-o outputfile]
[-p print statistics] [-b On error batch abort]
[-X[1] disable commands [and exit with warning]]
[-O use Old ISQL behavior disables the following]
      <EOF> batch processing
      Auto console width scaling
      Wide messages
      default errorlevel is -1 vs 1
[-? show syntax summary]

==============================================================================

C:/Documents and Settings/wangyd>sqlcmd/?
Microsoft (R) SQL Server Command Line Tool
Version 10.0.1600.22 NT INTEL X86
Copyright (c) Microsoft Corporation. All rights reserved.

usage: Sqlcmd            [-U login id]          [-P password]
[-S server]            [-H hostname]          [-E trusted connection]
[-d use database name] [-l login timeout]     [-t query timeout]
[-h headers]           [-s colseparator]      [-w screen width]
[-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
[-c cmdend]            [-L[c] list servers[clean output]]
[-q "cmdline query"]   [-Q "cmdline query" and exit]
[-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
[-u unicode output]    [-r[0|1] msgs to stderr]
[-i inputfile]         [-o outputfile]        [-z new password]
[-f <codepage> | i:<codepage>[,o:<codepage>]] [-Z new password and exit]
[-k[1|2] remove[replace] control characters]
[-y variable length type display width]
[-Y fixed length type display width]
[-p[1] print statistics[colon format]]
[-R use client regional setting]
[-b On error batch abort]
[-v var = "value"...] [-A dedicated admin connection]
[-X[1] disable commands, startup script, enviroment variables [and exit]]
[-x disable variable substitution]
[-? show syntax summary]

可见二者之间还是有非常多的相同之处的,下面说一下如何通过命令行方式修改SqlServer的管理员sa的密码,前提条件是要在SqlServer数据库的本机操作,采用OS认证模式才可以正常运行。

C:\Documents and Settings\Administrator>osql -E
1> sp_password null,'123123','sa'
2> go
Password changed.
1> exit

sa的密码改成了123123