Summary of common SQL Server Management commands

Source: Internet
Author: User
Tags sql server management

1. view the database version
Select @ version
2. view the operating system parameters of the machine where the database is located
Exec master .. xp_msver
3. view database startup parameters
Sp_configure
4. view the database startup time
Select convert (varchar (30), login_time, 120) from Master .. sysprocesses where spid = 1
View database server name and Instance name
Print 'server name ...... + convert (varchar (30), @ servername)
Print 'instance ......: '+ convert (varchar (30), @ servicename)
5. View All Database names and sizes
Sp_helpdb
SQL statement used to rename a database
Sp_renamedb 'old _ dbname', 'new _ dbname'
6. View logon information of all database users
Sp_helplogins
View the role information of all database users
Sp_helpsrvrolemember
Fixed the fix_orphan_user script or loneuser process that can be used to isolate users during server migration.
Change the user owner of a Data Object
Sp_changeobjectowner [@ objectname =] 'object', [@ newowner =] 'owner'
Note: changing any part of the object name may corrupt the script and stored procedure.
You can use the add_login_to_aserver script to back up the database user logon information on a server.
View object-level user permissions under a database
Sp_helprotect
7. view linked servers
Sp_helplinkedsrvlogin
View remote database user logon information
Sp_helpremotelogin
8. view the size of a data object in a database
Sp_spaceused @ objname
You can also use the sp_toptables process to view the maximum n tables (50 by default ).
View the index information of a data object in a database
Sp_helpindex @ objname
You can also use the sp_nchelpindex process to view more detailed indexes.
Sp_nchelpindex @ objname
Clustered indexes sort records in physical order and occupy less space.
We recommend that you use non-clustered indexes and constraints for tables with frequent key value DML operations. The fillfactor parameter uses the default value.
View the constraints of a data object in a database
Sp_helpconstraint @ objname
9. view all stored procedures and functions in the database
Use @ database_name
Sp_stored_procedures
View stored procedures and functions Source code
Sp_helptext [url = mailto: '@ procedure_name'] '@ procedure_name' [/url]
View the Data Object Name containing a string @ Str
Select distinct object_name (ID) from syscomments where text like [url = mailto: '% @ STR %'] '% @ STR %' [/url]
Create an encrypted stored procedure or function with the with encryption parameter before the
You can use sp_decrypt to decrypt encrypted stored procedures and functions.
10. View user and process information in the database
Sp_who
View information about active users and processes in the SQL Server database
Sp_who 'active'
View the locks in the SQL Server database
Sp_lock
Process No. 1--50 is used internally in the SQL server system. If the process no. is greater than 50, it is the user's connection process.
Spid is the process number, dbid is the database number, and objid is the data object number
View the SQL statement being executed by the Process
DBCC inputbuffer ()
We recommend that you use the improved sp_who3 process to directly view the SQL statements run by the process.
Sp_who3
Check the process of using sp_who_lock for deadlocks
Sp_who_lock
11. View and contract the database Article File Method
View the file size of all database documents
DBCC sqlperf (logspace)
If some files are large, shrink the database files in simple recovery mode. The unit of size of @ database_name_log after the contraction is M.
Backup log @ database_name with no_log
DBCC shrinkfile (@ database_name_log, 5)
13. Check where the database is located
Select * From sysfiles
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.