Monitor all current user information connected to SQL Server

Source: Internet
Author: User
Tags sql server books

Source: Monitor All current user information connected to SQL Server

if object_id('P_getlinkinfo','P') is  not NULL Drop procP_getlinkinfoGoCreate procP_getlinkinfo@dbnamesysname=NULL,--to check the name of the library, it is implied that all@includeip bit=0 --whether IP information is displayed as beginDeclare @dbid int Set @dbid=db_id(@dbname)if object_id('tempdb: #tb') is  not NULL Drop Table#tbif object_id('tempdb: #ip') is  not NULL Drop Table#ipCreate Table#tb (IDint Identity(1,1), dbname sysname,hostnamenchar( -), LoginNamenchar( -), net_addressnchar( A), Net_ipnvarchar( the), Prog_namenchar( -)) Insert  into#tb (hostname,dbname,net_address,loginname,prog_name)Select distincthostname,db_name(dbid), Net_address,loginame,program_name fromMaster.. sysprocesseswhereHostname!="' and(@dbid  is NULL ordbid=@dbid) if @includeip=0 GotoLb_show--do not show ipDeclare @sql varchar( -),@hostname nchar( -),@id int Create Table#ip (hostnamenchar( -), avarchar( $)) DeclareTbcursorLocal for Select distinctHostname from#tbOpenTBFetch Next  fromTb into @hostname  while @ @fetch_status=0 begin Set @sql='Ping'+@hostname+'-a-n 1-l 1' Insert#ip (a)execMaster.. xp_cmdshell@sql Update#ipSetHostname=@hostname whereHostname is NULL Fetch Next  fromTb into @hostname End Update#tbSetNet_ip=leftAPatindex('%:%'A-1)  from#tb AInner Join (SelectHostname,a=substringAPatindex('Ping statistics for%:%'A+ -, -)  from#ipwhereA like'Ping statistics for%:%') b onA.hostname=b.hostname lb_show:SelectId,dbname,hostname,loginname,net_address,net_ip,prog_name from#tbEnd Go execP_getlinkinfo@dbname='Master',@includeip=1
Msg 15281, Level 16, State 1, procedure xp_cmdshell, line 1th SQL Server blocked access to the process ' Sys.xp_cmdshell ' for component ' xp_cmdshell ' because this component has been shut down as part of this server's security configuration. System administrators can enable ' xp_cmdshell ' by using Sp_confi gure. For more information about enabling ' xp_cmdshell ', see "surface Area Configurator" in SQL Server Books Online.

SELECT * fromsys.configurationsORDER byname; sp_configure'Show advanced Options',1;GoReconfigure;Gosp_configure'xp_cmdshell',1;GoReconfigure;Go

Monitor all current user information connected to SQL Server

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.