sql server 查询任务管理器数据

系统 1631 0
      
USE master
go
IF OBJECT_ID ( ' tempdb..#temp ' ) IS NOT NULL
DROP TABLE # temp
CREATE TABLE # temp
(
id
INT IDENTITY ( 1 , 1 )
NOT NULL
,Proc_info
NVARCHAR ( MAX ) NULL
,
CONSTRAINT pk_id PRIMARY KEY ( id ASC )
)
-- insert data into temp table
INSERT INTO # temp
( Proc_info )
EXEC master..xp_cmdshell ' tasklist /v '

-- delete the wrong records
DELETE FROM # temp
WHERE Proc_info IS NULL
OR Proc_info LIKE ' %==========% '
OR ID = 2
GO
-- create a splite function
CREATE FUNCTION F_split_V2 ( @S NVARCHAR ( 4000 ), @i INT )
RETURNS NVARCHAR ( 100 )
AS
BEGIN
IF @i = 1 -- Image_name
BEGIN
IF PATINDEX ( ' %.exe% ' , @s ) = 0
SET @s = RTRIM ( LEFT ( @s , PATINDEX ( ' %[0-9]% ' , @s ) - 1 ))
ELSE
SET @s = RTRIM ( LEFT ( @s , PATINDEX ( ' %.exe% ' , @s ) + 4 ))
END
ELSE
IF @i = 2 -- Pid
BEGIN
IF PATINDEX ( ' %.exe% ' , @s ) = 0
SET @s = STUFF ( @s , 1 , PATINDEX ( ' %[0-9]% ' , @s ) - 1 , '' )
ELSE
SET @s = LTRIM ( SUBSTRING ( @s ,
PATINDEX ( ' %.exe% ' , @s ) + 4 ,
LEN ( @s )))

SELECT @s = RTRIM ( LEFT ( @s , PATINDEX ( ' %[^0-9]% ' , @s ) - 1 ))
END
ELSE
IF @i = 3 -- Session_name
BEGIN
IF PATINDEX ( ' %.exe% ' , @s ) = 0
SET @s = STUFF ( @s , 1 , PATINDEX ( ' %[0-9]% ' , @s ) - 1 ,
'' )
ELSE
SET @s = LTRIM ( SUBSTRING ( @s ,
PATINDEX ( ' %.exe% ' , @s )
+ 4 , LEN ( @s )))
SELECT @s = STUFF ( @s , 1 , PATINDEX ( ' %[^0-9]% ' , @s ) - 1 ,
'' )
,
@s = RTRIM ( LEFT ( @s ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 ))
END
ELSE
IF @i = 4 -- Session#
BEGIN
IF PATINDEX ( ' %.exe% ' , @s ) = 0
SET @s = STUFF ( @s , 1 ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 , '' )
ELSE
SET @s = LTRIM ( SUBSTRING ( @s ,
PATINDEX ( ' %.exe% ' , @s )
+ 4 , LEN ( @s )))
SELECT @s = STUFF ( @s , 1 ,
PATINDEX ( ' %[^0-9]% ' , @s ) - 1 ,
'' )
,
@s = STUFF ( @s , 1 ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 , '' )
,
@s = RTRIM ( LEFT ( @s ,
PATINDEX ( ' % % ' , @s ) - 1 ))
END
ELSE
IF @i = 5 -- memory
BEGIN
IF PATINDEX ( ' %.exe% ' , @s ) = 0
SET @s = STUFF ( @s , 1 ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 ,
'' )
ELSE
SET @s = LTRIM ( SUBSTRING ( @s ,
PATINDEX ( ' %.exe% ' ,
@s ) + 4 , LEN ( @s )))

SELECT @s = STUFF ( @s , 1 ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 ,
'' )
,
@s = STUFF ( @s , 1 ,
PATINDEX ( ' %[^0-9]% ' , @s )
- 1 , '' )
,
@s = STUFF ( @s , 1 ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 ,
'' )
,
@s = STUFF ( @s , 1 ,
PATINDEX ( ' %[1-9]% ' , @s ) - 1 ,
'' )
,
@s = RTRIM ( LEFT ( @s ,
PATINDEX ( ' %k% ' , @s )
- 1 ))
END
ELSE
IF @i = 6 -- user_name
SELECT @s = STUFF ( @s , 1 ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 ,
'' )
,
@s = STUFF ( @s , 1 ,
PATINDEX ( ' %[^0-9]% ' , @s )
- 1 , '' )
,
@s = STUFF ( @s , 1 ,
PATINDEX ( ' %[0-9]% ' , @s ) - 1 ,
'' )
,
@s = STUFF ( @s , 1 ,
PATINDEX ( ' %[1-9]% ' , @s ) - 1 ,
'' )
,
@s = RTRIM ( SUBSTRING ( @s ,
PATINDEX ( ' %\% ' ,
@s ) + 1 , 18 ))
ELSE
IF @i = 7 -- cup_time
SELECT @s = SUBSTRING ( @s ,
PATINDEX ( ' %[0-9]:[0-9][0-9]:[0-9][0-9]% ' ,
@s ), 8 )
ELSE
SELECT @s = RIGHT ( @s ,
CHARINDEX ( ' ' ,
REVERSE ( @s ), 3 )
- 1 )
RETURN @s
END
go

SELECT dbo.F_split_V2(Proc_info, 1 ) AS image_name
,dbo.F_split_V2(Proc_info,
2 ) AS PID
,dbo.F_split_V2(Proc_info,
3 ) AS session_name
,dbo.F_split_V2(Proc_info,
4 ) AS session#
,dbo.F_split_V2(Proc_info,
5 ) + ' K ' AS Memory
,dbo.F_split_V2(Proc_info,
6 ) AS User_name
,dbo.F_split_V2(Proc_info,
7 ) AS cpu_time
,
' xp_cmdshell '' taskkill -IM ' + dbo.F_split_V2(Proc_info, 1 ) + ' /f ''' AS script
FROM # temp

sql server 查询任务管理器数据


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论