像.net中的session一样,假设能知道了数据库中的sessionID,那全部的操作都能知道了,由于有了这个唯一的身份识别的标识。

能够做的事情有非常多,如:当前哪个用户在做什么操作,在运行什么sql, 又如一个比較大的逻辑中要分别运行非常多存储过程,

在运行这些存储过程的过程其中,你想知道当前运行的进度,SQLServer正在运行哪个段sql语句,那么通过sessionID是非常easy

就得到这些信息的。

SQL Server 得到SPID,唯一的sessionID:

SELECT @@SPID

曾经我一直不知道,近期又装了SQLServer2014,发现每开一个Query 界面就有一个ID出来。我就特别想知道怎么取sessionID.

以下的存储过程是用来查看哪些sessionID正在运行什么操作。

create PROC [dbo].[dba_WhatSQLIsExecuting]

AS



BEGIN

-- Do not lock anything, and do not get held up by any locks.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED





-- What SQL Statements Are Currently Running?

SELECT [Spid] = session_Id

, ecid

, [Database] = DB_NAME(sp.dbid)

, [User] = nt_username

, [Status] = er.status

, [Wait] = wait_type

, [Individual Query] = SUBSTRING (qt.text,

er.statement_start_offset/2,

(CASE WHEN er.statement_end_offset = -1

THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2

ELSE er.statement_end_offset END -

er.statement_start_offset)/2)

,[Parent Query] = qt.text

, Program = program_name

, Hostname

, nt_domain

, start_time

FROM sys.dm_exec_requests er

INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid

CROSS APPLY sys.dm_exec_sql_text(er.sql_handle)as qt

WHERE session_Id > 50 -- Ignore system spids.

AND session_Id NOT IN (@@SPID) -- Ignore this current statement.

--and DB_NAME(sp.dbid)='RangeCheckTool'

ORDER BY 1, 2

END

还能够參考以下的文章:

http://www.mssqltips.com/sqlservertip/1799/identify-last-statement-run-for-a-specific-sql-server-session/

Identify last statement run for a specific SQL Server session

Problem

I was reading a recent blog post from Pinal Dave, SQL Server MVP, regarding returning
information on the latest query executed for a given session
.  He offered up a couple options to return the last query statement executed, settling upon querying the sys.sysprocesses system compatibility view, but another way that this can be done is through
the Dynamic Management Views and Functions.  The process for doing so is quite straight-forward and works in all versions of Microsoft SQL Server since DMOs (dynamic management objects) were integrated into SQL Server.

Solution

Before proceeding we should take a second to explain what a session is.  In Microsoft SQL Server, a session is synonymous with a user process.  Previous to SQL 2005 sessions were referred to - and identified solely - as SPIDs (short for session
id).  A SPID uniquely identifies a session and a SPID is unique across the SQL Server instance.  In an attempt to conform SQL Server object identifiers to be more user-friendly and to standardize a naming convention across all system objects, sessions are
now identified across the DMO and system catalog views as session_id.  You'll see similar changes between previous versions of SQL Server and current versions where all object identifiers are concerned.

You can use the @@spid() system function to return the session_id of the current session as follows:

SELECT @@SPID

For my test I get session_id = 52.

So, now that we've identified what session_id uniquely identifies the session I'm using during this demonstration, I'll do a simple query against the Northwind database.

SELECT C.[CompanyName]  

FROM [Northwind].dbo.[Customers] C  

WHERE C.[City] = 'Berlin'  

ORDER BY [C].[CompanyName]

At this point I'll now open up a separate query window in SQL Server Management Studio.  If I now execute the first query above you'll see that this registers as a new session on the
SQL Server instance:

SELECT @@SPID

For my test I get session_id = 53

Now I can utilize the sys.dm_exec_connections Dynamic Management View, in conjunction
with thesys.dm_exec_sql_text Dynamic Management Function to return the last query statement executed against the SQL Server instance on a selected session. 
In all truth, you can return the last query executed on all sessions, but for the sake of this discussion we're limiting the results based upon the session_id (52) we've identified above.  I'll present the query, then we can examine in detail what it provides
for us.

SELECT DEST.TEXT  

FROM sys.[dm_exec_connections] SDEC 

 CROSS APPLY sys.[dm_exec_sql_text](SDEC.[most_recent_sql_handle]) AS DEST 

WHERE SDEC.[most_recent_session_id] = 52 

The output for this query shows the statement that was run for session_id 52.

So what just happened?  Simply-put, we returned the results from the sys.dm_exec_connections DMV, limiting the results by the session_id (52) we identified above.  We, submitted the value
contained in the most_recent_sql_handle column of this DMV to the sys.dm_exec_sql_text Dynamic Management Function.  That function then returned as text, the value of the sql_handle we passed to it.

So what is a sql_handle?  Think of a sql_handle as a unique identifier for a query that is unique across the entire SQL Server instance.  Just as a session_id uniquely identifies a session,
so does a sql_handle identify a query.  The actual value of the sql_handle column is very cryptic.  The value for the most_recent_sql_handle in this example is shown below:

SELECT SDEC.[most_recent_sql_handle], DEST.[text]  

FROM sys.[dm_exec_connections] SDEC 

 CROSS APPLY sys.[dm_exec_sql_text](SDEC.[most_recent_sql_handle]) AS DEST 

WHERE SDEC.[most_recent_session_id] = 52 

Here we can see the value of the sql_handle and the text translation.

The handle itself does not really do much for us without the function call that rationalizes it into the original query text.  As you can see though, this very simple query does provide
us with yet another option for returning information on what users are (or have been) doing on the SQL Server instances we support.

Next Steps

  • The Dynamic Management Objects have so much to offer the DBA.  Check out other tips on DMOs from MSSQLTips.com.
  • Read more tips by the author here.
  • Still interested in information on sysprocesses, whether as a system table (pre-SQL 2005) or system view?  Here are some tips that
    meet your needs.

SQL Server 得到SPID,唯一的sessionID的更多相关文章

  1. mysql,sql server,oracle 唯一索引字段是否允许出现多个 null 值?

    最近一个项目,涉及到sql server 2008,因为业务需求,希望建立一个唯一索引,但是发现在sql server中,唯一索引字段不能出现多个null值,下面是报错信息: CREATE UNIQU ...

  2. Sql Server 索引之唯一索引和筛选索引

    唯一索引(UNIQUE  INDEX) 当主键创建时如果不设置为聚集索引,那么就一定是唯一的非聚集索引.实际上,唯一索引,故名思议就是它要求该列上的值是唯一的.唯一索引能够保证索引键中不包含重复的值, ...

  3. 无法将数据库从SINGLE_USER模式切换回MULTI_USER模式(Error 5064),及查找SQL Server数据库中用户spid(非SQL Server系统spid)的方法

    今天公司SQL Server数据库无意间变为SINGLE_USER模式了,而且使用如下语句切换回MULTI_USER失败: ALTER DATABASE [MyDB] SET MULTI_USER W ...

  4. SQL Server索引进阶:第八级,唯一索引

    原文地址: Stairway to SQL Server Indexes: Level 8,Unique Indexes 本文是SQL Server索引进阶系列(Stairway to SQL Ser ...

  5. 【译】索引进阶(八):SQL SERVER唯一索引

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 在本章节我们检查唯一索引.唯一索引的特别之处在于它不仅提供了性能益处,而且提供了数据完整性益处.在SQL SER ...

  6. 安装SQL Server 2012 『企业中文版』

    安装 SQL Server 前,请详细参阅:计划安装SQL Server2012需求详细http://www.cnblogs.com/chhuang/p/3623198.html 安装 SQL Ser ...

  7. Microsoft SQL Server 博客目录

    基础概念篇 SQL Server排序规则 SQL SERVER 统计信息概述(Statistics) SQL SERVER 索引之聚集索引和非聚集索引的描述 Sql Server 索引之唯一索引和筛选 ...

  8. Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程

    原文:Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程 Red Gate系列之三 SQL S ...

  9. SQL Server 数据完整性的实现——约束

    SQL Server数据库采用的是关系数据模型,而关系数据模型本身的优点之一就是模型本身集成了数据完整性.作为模型一部分而实施的数据完整性(例如在创建数据表时的列属性定义)称作为声明式(Declara ...

随机推荐

  1. 【Java GUI】Java面板基础:JPanel

    有两个面板,常见的面板(JPanel)和滚动面板(JScrollPane) Jpanel 面板是一种常见的容器,JPanel的作用是实现接口层次结构,面放入一些组件.也能够在上面绘画,将放有组件和有画 ...

  2. 添加服务引用和添加Web引用对比

    原文:添加服务引用和添加Web引用对比 在WindowsForm程序中添加服务引用和Web引用对比 为了验证书上有关Visual Studio 2010添加服务引用和Web引用的区别,进行实验. 一. ...

  3. hibernate在地图的方法之一协会

    [Hibernate]之关于多对一单向关联映射 在项目的开发中多对一的单向关联映射是最常见的关联映射! 这个着重具体解说一下! 比如,我们如今一个组(Group)和人(Person) id name ...

  4. 使用shell命令分析统计日志

    用户需要登录统计信息,当分析用户行为,使用shell通常可以很容易地取出了大量的数据.删除,然后放入excel统计. 例如:统计日志含有loadCustomProcess这个地址的訪问,按訪问耗时排序 ...

  5. NOI第一天感想&小结

    嘛...中午总算是到了深圳了--在虹桥机场和飞机上和市队大神们一起讨论各种各样奇(sang)葩(bing)的算(ren)法(lei)还是非常开心的,在此再各种膜拜一下尽管没来比赛的FFT大神@陈中瑞 ...

  6. Fiddler工具的基本功能(转)

    Fiddler是一款用于网页数据分析,抓取的工具,里面集成了对网页强大的功能外,还可以通过设置,使其对手机的数据也可以进行抓取 Fiddler的原理是: 通过在客户端和服务器之间创建一个代理服务器来对 ...

  7. B/S在北大青鸟-ASP.NET 总结

    一个.前言: 这几周跟着于海涛老师进入了.NET编程世界.领略到了ASP.NET的精髓. 要说起ASP.NET的发展史,那要追溯到HTML了,由于它功能简单,无法从用户接收信息并自己主动进行更新.而不 ...

  8. ASP.NET MVC(C#)和Quartz.Net组件

    ASP.NET MVC(C#)和Quartz.Net组件 在之前的文章<推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler>和&l ...

  9. 你如何破解后安装PS cs6

    至于破解程序猿支持,资源共享是只有更好的,我相信有很多孩子还在用cs5看版本号.假设你想尝试新的版本号PS.但很长一段时间不能找到字的串行数.现在,你如何支付你的序列号和使用永久裂纹PS cs6. 好 ...

  10. 安装 CentOS 7 后必做的七件事

    原文 安装 CentOS 7 后必做的七件事 CentOS 是最多人用来运行服务器的 Linux 版本,最新版本是 CentOS 7.当你兴趣勃勃地在一台主机或 VPS 上安装 CentOS 7 后, ...