查询数据库后台Block 的Sql存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

/*记录SQL Server的阻塞情况 wang 2008.8.11

(@P1 nvarchar(8),@P2 nvarchar(42),@P3 int,@P4 nvarchar(42),@P5 int,@P6 int,@P7 nvarchar(8),@P8 int)
    SELECT TOP 1 A.ITEMID,A.STATUSISSUE,A.INTERCOMPANYINVENTDIMTRANSFER3,A.DATEPHYSICAL,A.QTY,A.COSTAMOUNTPOSTED,A.CURRENCYCODE,A.TRANSTYPE,A.TRANSREFID,A.INVOICEID,A.VOUCHER,A.DATEEXPECTED,A.DATEFINANCIAL,A.COSTAMOUNTPHYSICAL,A.INVENTTRANSID,A.STATUSRECEIPT,A.PACKINGSLIPRETURNED,A.INVOICERETURNED,A.PACKINGSLIPID,A.VOUCHERPHYSICAL,A.COSTAMOUNTADJUSTMENT,A.SHIPPINGDATEREQUESTED,A.SHIPPINGDATECONFIRMED,A.QTYSETTLED,A.COSTAMOUNTSETTLED,A.VALUEOPEN,A.DIRECTION,A.DATESTATUS,A.COSTAMOUNTSTD,A.DATECLOSED,A.INVENTTRANSIDFATHER,A.COSTAMOUNTOPERATIONS,A.ITEMROUTEID,A.ITEMBOMID,A.INVENTTRANSIDRETURN,A.INVENTDIMID,A.INVENTDIMFIXED,A.DATEINVENT,A.CUSTVENDAC,A.TRANSCHILDREFID,A.TRANSCHILDTYPE,A.TIMEEXPECTED,A.REVENUEAMOUNTPHYSICAL,A.ASSETID,A.TAXAMOUNTPHYSICAL,A.ASSETBOOKID,A.INVENTREFTRANSID,A.MODIFIEDDATE,A.MODIFIEDTIME,A.MODIFIEDBY,A.CREATEDDATE,A.CREATEDTIME,A.CREATEDBY,A.RECVERSION,A.RECID
    FROM INVENTTRANS A
        WHERE (([email protected]) AND (((([email protected]) AND ([email protected])) AND ([email protected]))
        AND (([email protected]) OR ([email protected]))))
        AND EXISTS (SELECT TOP 1 ‘x‘ FROM INVENTDIM B,INVENTLOCATION C WHERE (B.INVENTDIMID=A.INVENTDIMID) AND (([email protected]) AND ((C.INVENTLOCATIONID=B.INVENTLOCATIONID) AND ([email protected]))))
    --kill 282
  2008.9.6修改,将Blocked的数据另外增加新行保存,Blocked_xxx这些字段废弃

    exec axreport.[dbo].[UP_Log_ServerBlocked] 1,1
--    kill 394
    exec [dbo].[UP_Log_ServerSQL] ‘AXlivep1‘

    select top 10 object_Name(rsc_objid),* from sys.syslockinfo where req_spid=‘89‘

    select * from sys

    (@P1 ntext,@P2 ntext,@P3 ntext,@P4 ntext,@P5 ntext,@P6 nvarchar(42),@P7 nvarchar(122),@P8 nvarchar(22),@P9 int,@P10 int,@P11 int,@P12 int,@P13 int,@P14 datetime,@P15 numeric(28,12),@P16 numeric(28,12),@P17 datetime,@P18 datetime,@P19 datetime,@P20 datetime,@P21 datetime,@P22 numeric(28,12),@P23 numeric(28,12),@P24 numeric(28,12),@P25 numeric(28,12),@P26 numeric(28,12),@P27 datetime,@P28 int,@P29 int,@P30 nvarchar(42),@P31 nvarchar(22),@P32 nvarchar(22),@P33 nvarchar(22),@P34 nvarchar(22),@P35 nvarchar(22),@P36 nvarchar(22),@P37 nvarchar(22),@P38 nvarchar(22),@P39 int,@P40 nvarchar(42),@P41 nvarchar(42),@P42 int,@P43 nvarchar(42),@P44 datetime,@P45 int,@P46 nvarchar(22),@P47 int,@P48 datetime,@P49 int,@P50 int,@P51 nvarchar(22),@P52 numeric(28,12),@P53 nvarchar(42),@P54 nvarchar(42),@P55 nvarchar(22),@P56 nvarchar(42),@P57 int,@P58 int,@P59 datetime,@P60 nvarchar(42),@P61 nvarchar(42),@P62 int,@P63 int,@P64 int,@P65 int,@P66 nvarchar(42),@P67 int,@P68 nvarchar(32),@P69 int,@P70 nvarchar(42),@P71 int,@P72 numeric(28,12),@P73 int,@P74 datetime,@P75 nvarchar(50),@P76 datetime,@P77 int,@P78 datetime,@P79 datetime,@P80 numeric(28,12),@P81 numeric(28,12),@P82 numeric(28,12),@P83 numeric(28,12),@P84 nvarchar(26),@P85 numeric(28,12),@P86 numeric(28,12),@P87 nvarchar(22),@P88 numeric(28,12),@P89 numeric(28,12),@P90 numeric(28,12),@P91 numeric(28,12),@P92 numeric(28,12),@P93 numeric(28,12),@P94 numeric(28,12),@P95 nvarchar(42),@P96 int,@P97 int,@P98 datetime,@P99 datetime,@P100 int,@P101 numeric(28,12),@P102 numeric(28,12),@P103 int,@P104 datetime,@P105 int,@P106 int,@P107 nvarchar(26),@P108 datetime,@P109 int,@P110 int,@P111 int,@P112 datetime,@P113 int,@P114 int,@P115 nvarchar(42),@P116 numeric(28,12),@P117 nvarchar(22),@P118 nvarchar(22),@P119 nvarchar(22),@P120 datetime,@P121 int,@P122 int,@P123 int,@P124 int,@P125 numeric(28,12),@P126 numeric(28,12),@P127 int,@P128 nvarchar(512),@P129 datetime,@P130 int,@P131 nvarchar(12),@P132 datetime,@P133 int,@P134 nvarchar(12),@P135 nvarchar(8),@P136 int,@P137 bigint)
    INSERT INTO PRODTABLE (SML_REMARK,SML_PRODSTOPPEDREMARK,SML_PRODBOMREMARK,SML_PRODDELAYREMARK,SML_VENDREMARKS,ITEMID,NAME,PRODGROUPID,PRODSTATUS,PRODPRIO,PRODLOCKED,PRODTYPE,SCHEDSTATUS,SCHEDDATE,QTYSCHED,QTYSTUP,DLVDATE,STUPDATE,FINISHEDDATE,SCHEDSTART,SCHEDEND,HEIGHT,WIDTH,DEPTH,DENSITY,QTYCALC,REALDATE,RESERVATION,PRODPOSTINGTYPE,INVENTTRANSID,DIMENSION,DIMENSION2_,DIMENSION3_,DIMENSION4_,DIMENSION5_,DIMENSION6_,DIMENSION7_,DIMENSION8_,INVENTREFTYPE,INVENTREFID,INVENTREFTRANSID,COLLECTREFLEVEL,COLLECTREFPRODID,BOMDATE,BACKORDERSTATUS,PRODPOOLID,PROFITSET,CALCDATE,ROUTEJOBS,CHECKROUTE,PROPERTYID,REMAININVENTPHYSICAL,BOMID,ROUTEID,REQPLANIDSCHED,REQPOID,REFLOOKUP,LATESTSCHEDDIRECTION,LATESTSCHEDDATE,PRODID,INVENTDIMID,SCHEDTOTIME,SCHEDFROMTIME,LATESTSCHEDTIME,DLVTIME,PRODORIGID,GANTTCOLORID,PACKAGEQTY,SML_PRODSTOPPED,SML_REFREPLACEPRODID,SML_PRODTABLESTATUSTRACK,SML_PRODTOLERANCE,SML_PRODBOMAPPROVED,SML_ORIGDLVDATE,SML_PRODBOMAPPROVER,SML_PRODBOMAPPROVALDATE,SML_PRODBOMAPPROVALTIME,SML_PRODDLVDATE,SML_PRODURGENTORDERDATE,SML_PRODURGENTORDERQTY,SML_PLATEPCSROW,SML_PLATEPCSCOL,SML_PLATEPCSADD,SML_PATTNO,SML_LROWS_SS,SML_RIBBONS,SML_PRODDEMAND,SML_PLATESIZEWIDTH,SML_PLATESIZEHEIGHT,SML_PRODTOTALHRS,SML_PRODPLATECALCPRODQTY,SML_PRODPLATECALCPRINTRUN,SML_PRODPLATECALCPLATECNT,SML_PRODPLATECALCNOOFCOPIES,SML_REFREPLACEPARENTPRODID,SML_PRODPLATECALCULATED,SML_PNUM,SML_PRODOUTSOURCINGREPLYDATE,SML_URGENTORDERAPPROVALDATE,SML_TRADEMODE,SML_PRODPLATECALCPRINTRUNSCRAP,SML_PRODPLATECALCPRINTRUN30042,SML_TOELABEL,SML_ORDERCREATEDDATE,SML_PRODLEDTY,SML_PRODURGENTORDERTYPE,SML_PATTCODE,SML_REFFINISHEDDATE,SML_COUNTED,SML_INCLEPLATFORMPDF,SML_ISURGENTORDER,SML_MAINDLVDATE,SML_ISREPLACEPRODORDER,SML_ISPRODSYSRAF,SML_VENDACCOUNT,SML_OVERRAFQTY,SML_PRODMETHODID,SML_CLASSNO,WRKCTRID,SML_MAINFINISHEDDATE,SML_MAINFINISHEDTIME,SML_PPCTRL_TEST,SML_PPCTRL_QC,SML_PPCTRL_PROCESS,SML_OILCONTENT,SML_WEIGHTOFBOBBIN,SML_RETENTIONGOODSLEVEL,SML_PACKINGREMARKS,MO

*/
ALTER  Proc [dbo].[UP_Log_ServerBlocked]
  @LogSelfBlocked bit=0 ,
  @ShowOnly bit =0,
  @All bit =0
as
   set Nocount on
   Declare @Log_Date datetime,@Count int
   set @Log_Date=GetDate()
   select spid,Blocked=(Case when spid=Blocked then 0 Else Blocked end),
        db_name(dbid) as DBName,hostname,Loginame,
        program_name,Convert(nvarchar(4000),‘‘) as Spid_SQL ,
        Convert(nvarchar(4000),‘‘) as LockedObject ,
        --Convert(nvarchar(4000),‘‘) as Blocked_SQL,
        --Convert(nvarchar(256),‘‘) as Blocked_Loginame,Convert(nvarchar(256),‘‘) as Blocked_hostname,
        --Convert(nvarchar(256),‘‘) as Blocked_program_name,
        Waittime,cpu,physical_io,
        memusage,login_time,last_batch,open_tran,status,waittype,waitresource,Dbid,cmd
      into #Temp_ServerBlocked
    from sys.sysprocesses with(nolock)
    where @All=1 or spid in
       (select spid from sys.sysprocesses with(nolock)
             where blocked>0
        union
        select blocked from sys.sysprocesses with(nolock)
             where blocked>0
       )

   If @LogSelfBlocked=0
      Delete from #Temp_ServerBlocked where Blocked=0  and spid not in (select blocked from #Temp_ServerBlocked)
   Select @Count=count(1) from #Temp_ServerBlocked
   If @Count >0 Begin
      Declare @spid int,@Blocked int,@Spid_SQL nvarchar(4000),@LockedObject nvarchar(4000)
            --,@Blocked_SQL nvarchar(4000),@Blocked_Loginame nvarchar(128),
            --@Blocked_hostname nvarchar(128),@Blocked_program_name nvarchar(128)
      declare ccc cursor local fast_forward for
         select distinct spid,blocked from #Temp_ServerBlocked
      open ccc
      fetch ccc into @spid,@blocked
      while @@fetch_status=0 begin
         --user SQL
         --select @Blocked_Loginame=‘‘,@Blocked_hostname=‘‘,@Blocked_program_name=‘‘

         --select @Blocked_Loginame=Loginame,@Blocked_hostname=hostname,@Blocked_program_name=program_name
         --   from master..sysprocesses with(nolock) where [email protected]

         --set @Blocked_SQL=‘‘
         Exec dbo.UP_GetSPID_SQL @spid, @Spid_SQL output,@LockedObject output

         --If (@spid<>@Blocked) and @Blocked<>0
         --   Exec dbo.UP_GetSPID_SQL @Blocked, @Blocked_SQL output

         Update #Temp_ServerBlocked set Spid_SQL=@Spid_SQL
                        ,LockedObject=@LockedObject
                    --, [email protected]_SQL,[email protected]_Loginame,
                    --      [email protected]_hostname,[email protected]_program_name
            where spid=@spid and blocked=@blocked

         fetch ccc into @spid,@blocked
     end
     Close ccc
     Deallocate ccc
     select * from #Temp_ServerBlocked
     Drop table #Temp_ServerBlocked
   End

查询到后用kill spId eg: kill 992, 就可以清除block.

时间: 2024-10-01 12:24:09

查询数据库后台Block 的Sql存储过程的相关文章

Sql Server系列:SQL语句查询数据库中表、视图、存储过程等组成

1. 查看用户表 select name from sys.tables select name from sys.objects where type='U' select name from sysobjects where xtype='U' 其中type或xtype值:U = 用户表 S = 系统表 V = 视图 FN = 标量函数 TF = 表函数 P = 存储过程  TR = 触发器 D = 默认值或 DEFAULT 约束 PK = PRIMARY KEY 约束 F = FOREIG

SQL Server查询数据库近期执行的SQL语句

SELECT TOP 1000        ST.text AS '执行的SQL语句',       QS.execution_count AS '执行次数',       QS.total_elapsed_time AS '耗时',       QS.total_logical_reads AS '逻辑读取次数',       QS.total_logical_writes AS '逻辑写入次数',       QS.total_physical_reads AS '物理读取次数',    

SQL 中怎么查询数据库中具有的表、存储过程、试图数目、总触发器数、作业数

用户表:select count(*) 总表数 from sysobjects where xtype='u' 刚才那个是用户表,下面这个是系统表加用户表: select count(*) 总表数 from sysobject s where xtype in('u','s') 总视图数:select count(*) 总视图数 from sysobjects where xtype='v' 总存储过程数:select count(*) 总存储过程数 from sysobjects where

公交车路线查询系统后台数据库设计--换乘算法改进与优化

在<查询算法>一文中已经实现了换乘算法,但是,使用存储过程InquiryT2查询从“东圃镇”到“车陂路口”的乘车路线时,发现居然用了5分钟才查找出结果,这样的效率显然不适合实际应用.因此,有必要对原有的换乘算法进行优化和改进.在本文中,将给出一种改进的换乘算法,相比原有的算法,改进后的算法功能更强,效率更优. 1. “压缩”RouteT0 假设RouteT0有以下几行 如下图所示,当查询S1到S4的二次换乘路线时,将会产生3×2×4=24个结果 从图中可以看出,第1段路线中的3条线路的起点和站

MySQL数据库优化技术之SQL语句慢查询定位

通过show status命令了解各种SQL的执行频率 MySQL客户端连接成功后,通过使用show [session|global] status 命令可以提供服务器状态信息: 其中的session来表示当前的连接的统计结果,global来表示自数据库启动至今的统计结果,默认是session级别的. show status 常用命令: show status like 'com_%'; 其中com_xxx表示xxx语句所执行的次数: 重点注意com_select, com_insert, co

《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件

<万能数据库查询分析器>实现使用SQL语句直接高效地访问文本文件 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要    用SQL语句来直接访问文本文件?是在做梦吗? 本文详细地介绍了"万能数据库查询分析器",中文版本<DB 查询分析器>.英文版本<DB Query Analyzer>在 文本文件处理方面非常强大的功能,你可以直接用SQL语句来访问这些文本文件,访问250万条记录的文件的复杂的关联操作,也不过用时59秒钟.需要注意

SQL查询数据库是否存在

在实际工作中会遇到通过SQL查询数据库是否存在的情况,下面一些语句可以提供一些帮助,本文的语句是在SQL08R2中测试的 1,查询当前数据库服务器所有数据库 select *  From master.dbo.sysdatabases 2,查询数据库是否存在 select count(*) From master.dbo.sysdatabases where name='数据库名' 3,查询某数据库中所有用户表 select * from sysobjects where xtype='U' ‘

SQLSERVER查询数据库死锁的存储过程

原文:SQLSERVER查询数据库死锁的存储过程 USE [IdentityDemo] GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 2019/1/17 10:47:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE procedure [dbo].[sp_who_lock] as begin declare @spid in

SQL存储过程删除数据库日志文件的方法

--日志文件收缩至多少M DECLARE @DBLogSise AS INT SET @DBLogSise=0 --查询出数据库对应的日志文件名称 DECLARE @strDBName AS NVARCHAR(500) DECLARE @strLogName AS NVARCHAR(500) DECLARE @strSQL AS VARCHAR(1000) SELECT @strLogName=B.name, @strDBName=A.name FROM master.sys.databases