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

时间:2021-06-11 16:31:57

查询数据库后台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 ((A.DATAAREAID=@P1) AND ((((A.ITEMID=@P2) AND (A.TRANSTYPE=@P3)) AND (A.TRANSREFID=@P4))
AND ((A.STATUSRECEIPT=@P5) OR (A.STATUSRECEIPT=@P6))))
AND EXISTS (SELECT TOP 1 'x' FROM INVENTDIM B,INVENTLOCATION C WHERE (B.INVENTDIMID=A.INVENTDIMID) AND ((C.DATAAREAID=@P7) AND ((C.INVENTLOCATIONID=B.INVENTLOCATIONID) AND (C.INVENTLOCATIONTYPE=@P8))))
--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 Spid=@blocked --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
--, Blocked_SQL=@Blocked_SQL,Blocked_Loginame=@Blocked_Loginame,
-- Blocked_hostname=@Blocked_hostname,Blocked_program_name=@Blocked_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.