使用 SQL Server Management Studio (SSMS) 的提示和技巧Tips and tricks for using SQL Server Management Studio (SSMS)

03/13/2018

本文内容

本文介绍了一些使用 SQL Server Management StudioSQL Server Management Studio (SSMS) 的提示和技巧。This article gives you some tips and tricks for using SQL Server Management StudioSQL Server Management Studio (SSMS). 本文介绍如何:This article shows you how to:

注释/取消注释 Transact-SQL (T-SQL) 文本Comment/uncomment your Transact-SQL (T-SQL) text

缩进文本Indent your text

在对象资源管理器中筛选对象Filter objects in Object Explorer

访问 SQL ServerSQL Server 错误日志Access your SQL ServerSQL Server error log

查找 SQL ServerSQL Server 实例的名称Find the name of your SQL ServerSQL Server instance

先决条件Prerequisites

若要测试本文提供的步骤,必须有 SQL Server Management StudioSQL Server Management Studio、对 SQL Server 的访问权限,以及 AdventureWorks 数据库。To test out the steps provided in this article, you need SQL Server Management StudioSQL Server Management Studio, access to a SQL server, and an AdventureWorks database.

注释/取消注释 T-SQL 代码Comment/uncomment your T-SQL code

可使用工具栏中的“注释”按钮注释和取消注释部分文本 。You can comment and uncomment portions of your text by using the Comment button on the toolbar. 系统不会执行已注释掉的文本。Text that is commented out is not executed.

打开 SQL Server Management StudioSQL Server Management Studio。Open SQL Server Management StudioSQL Server Management Studio.

连接到 SQL Server。Connect to your SQL server.

打开“新建查询”窗口。Open a New Query window.

将以下 Transact-SQLTransact-SQL 代码粘贴到文本窗口。Paste the following Transact-SQLTransact-SQL code in your text window.

USE master

GO

-- Drop the database if it already exists

IF EXISTS (

SELECT name

FROM sys.databases

WHERE name = N'TutorialDB'

)

DROP DATABASE TutorialDB

GO

CREATE DATABASE TutorialDB

GO

ALTER DATABASE [TutorialDB] SET QUERY_STORE=ON

GO

突出显示文本的“更改数据库”部分,然后选择工具栏中的“注释”按钮 :Highlight the Alter Database portion of the text, and then select the Comment button on the toolbar:

选择“执行”运行取消注释的文本部分 。Select Execute to run the uncommented portion of the text.

突出显示除“更改数据库”命令之外的所有内容,然后选择“注释”按钮 :Highlight everything except for the Alter Database command, and then select the Comment button:

备注

注释的文本的键盘快捷方式是 CTRL + K,CTRL + C 。The keyboard shortcut to comment text is CTRL + K, CTRL + C.

突出显示文本的“更改数据库”部分,然后选择工具栏中的“取消注释”按钮以取消注释 :Highlight the Alter Database portion of the text, and then select the Uncomment button to uncomment it:

备注

取消注释的文本的键盘快捷方式是 CTRL + K,CTRL + U 。The keyboard shortcut to uncomment text is CTRL + K, CTRL + U.

选择“执行”运行取消注释的文本部分 。Select Execute to run the uncommented portion of the text.

缩进文本Indent your text

可使用工具栏上的缩进按钮增加或减少文本的缩进。You can use the indentation buttons on the toolbar to increase or decrease the indent of your text.

打开“新建查询”窗口。Open a New Query window.

将以下 Transact-SQLTransact-SQL 代码粘贴到文本窗口:Paste the following Transact-SQLTransact-SQL code in your text window:

USE master

GO

--Drop the database if it already exists

IF EXISTS (

SELECT name

FROM sys.databases

WHERE name = N'TutorialDB'

)

DROP DATABASE TutorialDB

GO

CREATE DATABASE TutorialDB

GO

ALTER DATABASE [TutorialDB] SET QUERY_STORE=ON

GO

突出显示文本的“更改数据库”部分,然后选择工具栏上的“增加缩进”按钮以向前移动此文本 :Highlight the Alter Database portion of the text, and then select the Increase Indent button on the toolbar to move this text forward:

再次突出显示文本的“更改数据库”部分,然后选择“减少缩进”按钮以向后移动此文本 。Highlight the Alter Database portion of the text again, and then select the Decrease Indent button to move this text back.

在对象资源管理器中筛选对象Filter objects in Object Explorer

在具有多个对象的数据库中,可以使用筛选功能来搜索特定表、视图等。本节介绍如何筛选表,但可在对象资源管理器中的任何其他节点中使用以下步骤:In databases that have many objects, you can use filtering to search for specific tables, views, etc. This section describes how to filter tables, but you can use the following steps in any other node in Object Explorer:

连接到 SQL Server。Connect to your SQL server.

展开“数据库” > “AdventureWorks” > “表” 。Expand Databases > AdventureWorks > Tables. 此时将显示数据库中的所有表。All the tables in the database appear.

右键单击“表”,然后选择“筛选器” > “筛选器设置” :Right-click Tables, and then select Filter > Filter Settings:

在“筛选器设置”窗口中,可以修改以下某些筛选器设置 :In the Filter Settings window, you can modify some of the following filter settings:

按名称筛选:Filter by name:

按架构筛选:Filter by schema:

若要清除筛选器,请右键单击“表”,然后选择“删除筛选器” 。To clear the filter, right-click Tables, and then select Remove Filter.

访问 SQL Server 错误日志Access your SQL Server error log

错误日志是一个文件,其中包含 SQL ServerSQL Server 实例中所发生操作的相关详细信息。The error log is a file that contains details about things that occur in your SQL ServerSQL Server instance. 可浏览和查询 SSMS 中的错误日志。You can browse and query the error login SSMS. 错误日志是位于磁盘上的日志文件。The error log is a .log file that's located on your disk.

在 SSMS 中打开错误日志Open the error log in SSMS

连接到你的 SQL ServerSQL Server。Connect to your SQL ServerSQL Server.

展开“管理” > “SQL Server 日志” 。Expand Management > SQL Server Logs.

右键单击“当前”错误日志,然后选择“查看 SQL Server 日志” :Right-click the Current error log, and then select View SQL Server Log:

在 SSMS 中查看查询日志Query the error log in SSMS

连接到 SQL Server。Connect to your SQL server.

打开“新建查询”窗口。Open a New Query window.

将以下 Transact-SQLTransact-SQL 代码粘贴到查询窗口:Paste the following Transact-SQLTransact-SQL code in your query window:

sp_readerrorlog 0,1,'Server process ID'

将单引号中的文本修改为要搜索的文本。Modify the text in the single quotes to text you want to search for.

执行查询然后查看结果:Execute the query, and then review the results:

如果连接到 SQL Server,请查找错误日志位置Find the error log location if you're connected to SQL Server

连接到你的 SQL ServerSQL Server。Connect to your SQL ServerSQL Server.

打开“新建查询”窗口。Open a New Query window.

将以下 Transact-SQLTransact-SQL 代码粘贴到查询窗口,然后选择“执行”:Paste the following Transact-SQLTransact-SQL code in your query window, and then select Execute:

SELECT SERVERPROPERTY('ErrorLogFileName') AS 'Error log file location'

结果将显示文件系统中错误日志的位置:The results show the location of the error log in the file system:

如果无法连接到 SQL Server,请查找错误日志位置Find the error log location if you can't connect to SQL Server

你的 SQL ServerSQL Server 错误日志的路径可能有所不同,具体取决于你的配置设置。The path for your SQL ServerSQL Server error log can vary depending on your configuration settings. 可以在 SQL Server 配置管理器内的启动参数中找到错误日志位置的路径。The path for the error log location can be found in the startup parameters within the SQL Server Configuration Manager. 请按照以下步骤来找到标识 SQL ServerSQL Server 错误日志位置的相关启动参数。Follow the steps below to locate the relevant startup parameter identifying the location of your SQL ServerSQL Server error log. 你的路径可能与以下指示的路径有所不同 。Your path may vary from the path indicated below.

打开“SQL Server 配置管理器”。Open SQL Server Configuration Manager.

展开“服务” 。Expand Services.

右键单击你的 SQL ServerSQL Server 实例,然后选择“属性”:Right-click your SQL ServerSQL Server instance, and then select Properties:

选择“启动参数”选项卡 。Select the Startup Parameters tab.

在“现有参数”区域中,“-e”后面的路径是错误日志的位置 :In the Existing Parameters area, the path after "-e" is the location of the error log:

此位置中包含多个错误日志文件。There are several error log files in this location. 当前错误日志的文件名以 *.log 结尾。The file name that ends with *.log is the current error log file. 以前的日志文件的文件名以数字结尾。File names that end with numbers are previous log files. 每次重新启动 SQL Server 时都会创建一个新日志。A new log is created every time the SQL server restarts.

在记事本中打开 errorlog.log 文件。Open the errorlog.log file in Notepad.

查找 SQL Server 实例名称Find SQL Server instance name

在连接到 SQL Server 之前和之后,有几个选项可用于查找 SQL ServerSQL Server 的名称。You have a few options for finding the name of your SQL server before and after you connect to SQL ServerSQL Server.

连接到 SQL Server 之前Before you connect to SQL Server

Follow the steps to locate the SQL Server error log on disk. 你的路径可能与下图中的路径有所不同。Your path may vary from the path in the image below.

在记事本中打开 errorlog.log 文件。Open the errorlog.log file in Notepad.

搜索文本“服务器名称是” 。Search for the text Server name is.

单引号中列出的所有内容都是将连接到的 SQL ServerSQL Server 实例的名称:Whatever is listed in the single quotes is the name of the SQL ServerSQL Server instance that you'll be connecting to:

名称的格式为 HOSTNAME\INSTANCENAME。The format of the name is HOSTNAME\INSTANCENAME. 如果只看到了主机名,然后已安装了默认实例,则实例名称是 MSSQLSERVER。If you see only the host name, then you've installed the default instance and your instance name is MSSQLSERVER. 连接到默认实例时,只需输入主机名以连接到 SQL Server。When you connect to a default instance, the host name is all you need to enter to connect to your SQL server.

连接到 SQL Server 时When you're connected to SQL Server

连接到 SQL ServerSQL Server 时,可在三个位置找到服务器名称:When you're connected to SQL ServerSQL Server, you can find the server name in three locations:

服务器名称将在“对象资源管理器”中列出:The name of the server is listed in Object Explorer:

服务器名称将在查询窗口中列出:The name of the server is listed in the Query window:

服务器名称将在“属性”中列出 。The name of the server is listed in Properties.

在“视图”菜单上,选择“属性窗口” :In the View menu, select Properties Window:

如果连接到别名或可用性组侦听程序If you're connected to an alias or Availability Group listener

如果连接到别名或可用性组侦听程序,则将在“对象资源管理器”和“属性”中显示该信息。If you're connected to an alias or to an Availability Group listener, that information appears in Object Explorer and Properties. 在这种情况下,SQL ServerSQL Server 名称可能不是显而易见的,并且必须进行查询:In this case, the SQL ServerSQL Server name might not be readily apparent, and must be queried:

连接到 SQL Server。Connect to your SQL server.

打开“新建查询”窗口。Open a New Query window.

将以下 Transact-SQLTransact-SQL 代码粘贴到窗口中:Paste the following Transact-SQLTransact-SQL code in the window:

select @@Servername

查看查询结果,确定连接到的 SQL ServerSQL Server 实例的名称:View the results of the query to identify the name of the SQL ServerSQL Server instance you're connected to:

后续步骤Next steps

熟悉 SSMS 的最好方式是进行实践演练。The best way to get acquainted with SSMS is through hands-on practice. 这些教程 和操作说明 文章可帮助你使用 SSMS 的各种功能。These tutorial and how-to articles help you with various features available within SSMS. 这些文章教你如何管理 SSMS 组件,以及如何查找常用功能。These articles teach you how to manage the components of SSMS and how to find the features that you use regularly.

ssms2008 代码自动提示_使用 SSMS 的提示和技巧 - SQL Server Management Studio (SSMS) | Microsoft Docs...相关推荐

  1. SQL Server Management Studio (SSMS)

    SQL Server Management Studio (SSMS) 官网: https://docs.microsoft.com/zh-cn/sql/ssms/sql-server-managem ...

  2. 下载 SQL Server Management Studio (SSMS)

    SSMS 是一种集成环境,用于管理从 SQL Server 到 SQL 数据库的任何 SQL 基础结构. SSMS 提供用于配置.监视和管理 SQL 实例的工具. 使用 SSMS 部署.监视和升级应用 ...

  3. SQL Server Management Studio (SSMS)单独安装,仅安装连接工具

    简单来说,SSMS是用于远程连接数据库与执行管理任务的一个工具.当安装SQL SERVER时,会默认安装.但也可以单独安装在不是数据库服务器的主机上. SQL Server Management St ...

  4. 使用 SQL Server Management Studio (SSMS) 连接 SQL Server实例

    参考资料 微软官方教程https://docs.microsoft.com/zh-cn/sql/ssms/tutorials/connect-query-sql-server?view=sql-ser ...

  5. [Microsoft SQL Server Management Studio]SSMS查询年龄最大学生的姓名和年龄问题及查询最高的学生的学号问题分析及解决

    查询年龄最大学生的姓名和年龄问题 问题描述 使用以下语句查询年龄最大学生的姓名和年龄的时候系统报错,为什么?要如何修改? SELECT StName, MAX(YEAR(GETDATE())-YEAR ...

  6. 在SQL Server Management Studio(SSMS)中调试存储过程

    Debugging is one of the most important but painful parts of any software process. To find some error ...

  7. Microsoft SQL Server Management Studio(SSMS)概述

    介绍 (Introduction) Microsoft SQL Server Server Management is an advanced development environment that ...

  8. SQL Server Management Studio中SQL代码段

    Snippets are a great productivity feature to speed up typing of repetitive T-SQL. Snippets were intr ...

  9. 如何使用SQL Server Management Studio(SSMS)连接到Azure存储帐户

    介绍 (Introduction) In SQL Server Management Studio (SSMS), it is possible to connect to the Azure Sto ...

最新文章

  1. python中tolist_python - 无法使用Gremlinpython使用“ .toList()”列出Janusgraph中存在的所有顶点 - 堆栈内存溢出...
  2. 不同国别的买家,谈判方式竟有如此大差异!
  3. unity集成openinstall流程
  4. python封装函数、实现将任意的对象序列化到磁盘上_序列化(serialization)
  5. 肾炎治疗有效方(湿热壅滞三焦,气机不利)
  6. 面向对象3(final、static、instanceof、向上/向下转型、初始化次序)
  7. 写给MongoDB开发者的50条建议Tip11
  8. C和指针之字符串编程练习10(判断字符串是否是回文数)
  9. OpenHub框架进行的异步通信
  10. (8)FPGA实现1s闪灯代码(学无止境)
  11. 前端 vue antdv table导出execl
  12. 2020杭电多校6 1006A Very Easy Graph Problem血泪史
  13. eclipse中SVN分支合并到主干
  14. 13、TORCH.OPTIM
  15. 【Excel】取消合并单元格后快速填充
  16. 推荐一本Vue开发的书籍
  17. WBADMIN 命令手册
  18. 导出chrome扩展插件,crx文件
  19. [高项]应急储备VS管理储备
  20. 单电源运放和双电源运放的区别

热门文章

  1. AWS Elasticsearch后模式
  2. javafx canvas_JavaFX技巧1:可调整大小的Canvas
  3. 使用混合多云每个人都应避免的3个陷阱(第3部分)
  4. Java命令行界面(第12部分):CLAJR
  5. JLBH示例3 –吞吐量对延迟的影响
  6. osgi 模块化_OSGI –模块化您的应用程序
  7. Java中的线程本地存储
  8. 使用Maven原型高效创建Eclipse模块
  9. Java验证(javafx)
  10. 具有Java 8支持的Spring Framework 4.0.3和Spring Data Redis 1.2.1