Renaming and error analysis of MSSQL database

Source: Internet
Author: User

Renaming and error analysis of MSSQL database

1. View the version of the database
SELECT @ @version
Several common SQL Server patched version numbers:
8.00.194 Microsoft SQL Server 2000
8.00.384 Microsoft SQL Server SP1
8.00.532 Microsoft SQL Server SP2
8.00.760 Microsoft SQL Server SP3
8.00.818 Microsoft SQL Server SP3 w/cumulative Patch ms03-031
8.00.2039 Microsoft SQL Server SP4
2. View the machine operating system parameters of the database
EXEC master.. xp_msver
3. View Database Startup Parameters
sp_configure
4. View Database Startup time
Select CONVERT (varchar, login_time,120) from master. sysprocesses

where spid=1
To view the database server name and instance name
print ' Server Name ... ...: ' + CONVERT (varchar (), @ @SERVERNAME), ".


print ' Instance ....: ' + CONVERT (varchar), @ @SERVICENAME. @)., d..


5. View all database names and sizes
sp_helpdb
To rename SQL for a database
Sp_renamedb ' Old_dbname ', ' new_dbname '
6. View all database user login information
Sp_helplogins
View the role information that all database users belong to
Sp_helpsrvrolemember
Fix_orphan_user scripts or Loneuser procedures that can be used when repairing orphaned users when migrating servers
Change the user owner of a data object
sp_changeobjectowner [@objectname =] ' object ', [@newowner =] ' owner '
Note: Changing any part of an object name can corrupt scripts and stored procedures.
To back up the database user login information on a single server, you can use Add_login_to_aserver script
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 under a database
sp_spaceused @objname
You can also see the largest n (default 50) table with the Sp_toptables procedure
View index information for a data object under a database
Sp_helpindex @objname
You can also use the Sp_nchelpindex procedure to view more detailed indexing
Sp_nchelpindex @objname
Clustered index is the physical order of records, the index occupies less space.
Tables with very frequent key-value DML operations I recommend using clustered indexes and constraints, FILLFACTOR parameters are

With the default value.
To view the constraint information for a data object under a database
Sp_helpconstraint @objname
9. View all stored procedures and functions in the database
Use @database_name
Sp_stored_procedures
To view the source code for stored procedures and functions
Sp_helptext ' @procedure_name '
View the name of a data object that contains a string @str
SELECT DISTINCT object_name (ID) from syscomments where text like '% @str% '
Create an encrypted stored procedure or function with the with encryption parameter in front of AS
Decryption of encrypted stored procedures and functions can be done using the Sp_decrypt procedure
10. View information about users and processes in the database
sp_who
View information about active users and processes in the SQL Server database
sp_who ' Active '
To view the locks in the SQL Server database
Sp_lock
The process number 1--50 is used internally within the SQL Server system, and the process number greater than 50 is the user's connection process.
The SPID is the process number, dbid is the database number, ObjID is the data object number
To view the SQL statement that the process is executing
DBCC INPUTBUFFER ()
We recommend that you use the improved SP_WHO3 process to see directly the SQL statements that the process runs
Sp_who3
Check for deadlocks with the Sp_who_lock process
Sp_who_lock
11. How to view and shrink database log files
View all database log file sizes
DBCC SQLPERF (LOGSPACE)
If some log files are large, shrink the simple recovery mode database log, after shrinking

The

@database_name_log size unit is m
   backup log @database_name with NO_LOG
   DBCC SHRINKFILE (@d Atabase_name_log, 5
12. Methods for parsing SQL Server SQL statements:
   SET STATISTICS time {on | off}
   SE T statistics io {on | off}
   graphical display query execution plan
   estimate plan for-> query-> display estimates in Query Analyzer (D)-ctrl-l or click on the toolbar Graphics
   text mode display Query execution plan
   set SHOWPLAN_ALL {on | off}
   set SHOWPLAN_TEXT {on | OFF}
   SET statistics profiles {on | off}
13. When there is an inconsistency error, error No. 3624 in the NT Event Viewer, the method to repair the database
   first Comment out the tables referenced in the application for inconsistencies, and then restore the

on backup or other machines.

Redo and do the repair operation
ALTER DATABASE [@error_database_name] set Single_user
To fix a table that appears inconsistent errors
DBCC CHECKTABLE (' @error_table_name ', Repair_allow_data_loss)
Or, unfortunately, choose to fix a small database name with inconsistent errors
DBCC CHECKDB (' @error_database_name ', Repair_allow_data_loss)
ALTER DATABASE [@error_database_name] set Multi_user
CHECKDB has 3 parameters:
Repair_allow_data_loss includes assigning and unassign rows and pages to correct allocation errors, structure

row or page errors, and deleting corrupted text objects, these fixes can cause some data loss.
A repair operation can be completed under a user transaction to allow the user to roll back the changes.
If you roll back the repair, the database will still contain errors and should be recovered from the backup.
If an incorrect fix is omitted due to the level of repair provided, any errors that will be omitted depend on the repair

of repair.
When the repair is complete, back up your database.
Repair_rest for small, time-consuming fixes, such as fixing additional keys in a nonclustered index.
These fixes can be done quickly, and there is no risk of losing data.
Repair_rebuild performs all repairs performed by repair_rest, including repairs that require a longer period of time

(such as rebuilding an index).
There is no risk of losing data when performing these repairs.

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.