用sql語(yǔ)句dbcc-log-查看SQL-Server-數(shù)據(jù)庫(kù)的事務(wù)日志_第1頁(yè)
用sql語(yǔ)句dbcc-log-查看SQL-Server-數(shù)據(jù)庫(kù)的事務(wù)日志_第2頁(yè)
用sql語(yǔ)句dbcc-log-查看SQL-Server-數(shù)據(jù)庫(kù)的事務(wù)日志_第3頁(yè)
用sql語(yǔ)句dbcc-log-查看SQL-Server-數(shù)據(jù)庫(kù)的事務(wù)日志_第4頁(yè)
用sql語(yǔ)句dbcc-log-查看SQL-Server-數(shù)據(jù)庫(kù)的事務(wù)日志_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

用sql語(yǔ)句dbcclog查看SQLServer數(shù)據(jù)庫(kù)的事務(wù)日志

1)用系統(tǒng)函數(shù)select

*

fromfn_dblog(null,null)2)用DBCCdbcclog(dbname,4)

--(n=0,1,2,3,4)1-更多信息plusflags,tags,rowlength2-非常詳細(xì)的信息plusobjectname,indexname,pageid,slotid3-每種操作的全部信息4-每種操作的全部信息加上該事務(wù)的16進(jìn)制信息

默認(rèn)type=0

要查看MSATER數(shù)據(jù)庫(kù)的事務(wù)日志可以用以下命令:DBCClog(master)使用fn_dblog解析SQLSERVER數(shù)據(jù)庫(kù)日志方法一直以來(lái)我都很困惑,不知道怎么解析SQLSERVER的日志,因?yàn)槲④浱峁┝薴n_dblog(NULL,NULL)和DBCCLOG獲取數(shù)據(jù)庫(kù)日志的基本信息,但是都是二進(jìn)制碼,看不懂。最近終于成功解析了SQLSERVERLOG信息在fn_dblog(NULL,NULL)輸出結(jié)果中,獲取表名是AllocUnitName字段。具體獲取方法:AllocUnitNamelike'dbo.TEST%'操作類(lèi)型是:Operation數(shù)據(jù)是:[RowLogContents0]字段內(nèi)容如果是UPDATE操作:修改后數(shù)據(jù)存放在[RowLogContents1]字段內(nèi)最基本3種操作類(lèi)型:'LOP_INSERT_ROWS','LOP_DELETE_ROWS','LOP_MODIFY_ROW'具體解析代碼如下:--解析日志

createfunctiondbo.f_splitBinary(@svarbinary(8000))

returns@ttable(idintidentity(1,1),Valuebinary(1))

as

begin

declare@iint,@imint

select@i=1,@im=datalength(@s)

while@i<=@im

begin

insertinto@tselectsubstring(@s,@i,1)

set@i=@i+1

end

return

endGOcreatefunctiondbo.f_reverseBinary(@svarbinary(128))

returnsvarbinary(128)

as

begin

declare@rvarbinary(128)

set@r=0x

select@r=@r+Valuefromdbo.f_splitBinary(@s)aorderbyiddesc

return@r

endGO

createproc[dbo].[p_getLog](@TableNamesysname,@cint=100)

/*

解析日志

:

p_getLog'tablename';*/

as

setnocounton

declare@svarbinary(8000),@s1varbinary(8000),@strvarchar(8000),@str1varchar(8000),@lbint,@leint,@operationvarchar(128)

declare@iint,@libint,@lieint,@ibint,@ieint,@lenVarint,@columnnamesysname,@lengthint,@columntypevarchar(32),@precint,@scaleint

declare@TUVLengthint,@vcint,@tcint,@bitAddint,@bitCountint,@countintselect,b.length,typename,b.colid,b.xprec,b.xscale,

casewhennotlike'%var%'andnotin('xml','text','image')then1else2endp,row_number()over(partitionby

casewhennotlike'%var%'andnotin('xml','text','image')then1else2endorderbycolid)pid

into#t

fromsysobjectsainnerjoinsyscolumnsbona.id=b.idinnerjoinsystypesconb.xtype=c.xusertype

=@TableNameorderbyb.colidSELECTtop(@c)Operation,[RowLogContents0],[RowLogContents1],[RowLogContents2],[RowLogContents3],[LogRecord],id=identity(int,1,1)into#t1

from::fn_dblog(null,null)

whereAllocUnitNamelike'dbo.'+@TableName+'%'and

Operationin('LOP_INSERT_ROWS','LOP_DELETE_ROWS','LOP_MODIFY_ROW')

ANDContextnotin('LCX_IAM','LCX_PFS')

orderby[CurrentLSN]descselect@tc=count(*)from#tselect@lb=min(id),@le=max(id)from#t1

while@lb<=@le

begin

select@operation=Operation,@s=[RowLogContents0],@s1=[RowLogContents1]from#t1whereid=@lbAND[RowLogContents1]ISNOTNULL

set@TUVLength=convert(int,dbo.f_reverseBinary(substring(@s,3,2)))+3

select@i=5,@str='',@vc=0,@bitCount=0

select@lib=min(pid),@lie=max(pid)from#twherep=1

while@lib<=@lie

begin

select@columnname=name,@length=length,@columntype=typename,@prec=xprec,@scale=xscale,@vc=colid-1from#twherep=1andpid=@lib

--

if@columntype<>'bit'

--

printrtrim(@i)+'->'+rtrim(@length)

ifdbo.f_reverseBinary(substring(@s,@TUVLength,1+((@tc-1)/8)))&power(2,@vc)<>0

begin

if@columntype<>'bit'

select@str=@str+@columnname+'=NULL,',@i=@i+@length

else

begin

select@str=@str+@columnname+'=NULL,'

set@bitAdd=casewhen@bitCount=0then@ielse@bitAddend

set@bitCount=(@bitCount+1)%8

set@i=@i+case@bitCountwhen1then1else0end

--

printrtrim(@bitAdd)+'->'+rtrim(@length)

end

end

elseif@columntype='char'

select@str=@str+@columnname+'='+convert(varchar(256),substring(@s,@i,@length))+',',@i=@i+@length

elseif@columntype='nchar'

select@str=@str+@columnname+'='+convert(nvarchar(256),substring(@s,@i,@length))+',',@i=@i+@length

elseif@columntype='datetime'

select@str=@str+@columnname+'='+convert(varchar,dateadd(second,convert(int,dbo.f_reverseBinary(substring(@s,@i,4)))/300

,dateadd(day,convert(int,dbo.f_reverseBinary(substring(@s,@i+4,4))),'1900-01-01')),120)+',',@i=@i+8

elseif@columntype='smalldatetime'

select@str=@str+@columnname+'='+convert(varchar,dateadd(minute,convert(int,dbo.f_reverseBinary(substring(@s,@i,2)))

,dateadd(day,convert(int,dbo.f_reverseBinary(substring(@s,@i+2,2))),'1900-01-01')),120)+',',@i=@i+4

elseif@columntype='int'

select@str=@str+@columnname+'='+rtrim(convert(int,dbo.f_reverseBinary(substring(@s,@i,4))))+',',@i=@i+4

elseif@columntype='decimal'

select@str=@str+@columnname+'=DECIMAL,',@i=@i+@length

elseif@columntype='bit'

begin

set@bitAdd=casewhen@bitCount=0then@ielse@bitAddend

set@bitCount=(@bitCount+1)%8

select@str=@str+@columnname+'='+rtrim(convert(bit,substring(@s,@bitAdd,1)&power(2,case@bitCountwhen0then8else@bitCountend-1)))+','

,@i=@i+case@bitCountwhen1then1else0end

--

printrtrim(@bitAdd)+'->'+rtrim(@length)

end

set@lib=@lib+1

end

set@i=convert(int,dbo.f_reverseBinary(substring(@s,3,2)))+4+((@tc-1)/8)

set@lenVar=convert(int,dbo.f_reverseBinary(substring(@s,@i,2)))

set@i=@i+2

set@ib=@i+@lenVar*2

set@ie=convert(int,dbo.f_reverseBinary(substring(@s,@i,2)))

set@count=0

select@lib=min(pid),@lie=max(pid)from#twherep=2

while@lib<=@lie

begin

--

printrtrim(@ib)+'->'+rtrim(@ie)

select@columnname=name,@length=length,@columntype=typename,@vc=colid-1from#twherep=2andpid=@lib

ifdbo.f_reverseBinary(substring(@s,@TUVLength,1+((@tc-1)/8)))&power(2,@vc)<>0

begin

select@str=@str+@columnname+'=NULL,'

select@ib=@ie+1,@i=@i+2

if@count<@lenVar

set@ie=convert(int,dbo.f_reverseBinary(substring(@s,@i,2)))

end

elseif@columntype='varchar'

begin

select@str=@str+@columnname+'='+convert(varchar(256),substring(@s,@ib,@ie-@ib+1))+','

select@ib=@ie+1,@i=@i+2

set@ie=convert(int,dbo.f_reverseBinary(substring(@s,@i,2)))

end

elseif@columntype='nvarchar'

begin

select@str=@str+@columnname+'='+convert(nvarchar(256),substring(@s,@ib,@ie-@ib+1))+','

select@ib=@ie+1,@i=@i+2

set@ie=convert(int,dbo.f_reverseBinary(substring(@s,@i,2)))

end

set@count=@count+1

set@lib=@lib+1

end

set@str=left(@str,len(@str)-1)

IF@operation='LOP_MODIFY_ROW'

BEGIN

set@TUVLength=convert(int,dbo.f_reverseBinary(substring(@s1,3,2)))+3

select@i=5,@str1='',@vc=0,@bitCount=0

select@lib=min(pid),@lie=max(pid)from#twherep=1

while@lib<=@lie

begin

select@columnname=name,@length=length,@columntype=typename,@prec=xprec,@scale=xscale,@vc=colid-1from#twherep=1andpid=@lib

--

if@columntype<>'bit'

--

printrtrim(@i)+'->'+rtrim(@length)

ifdbo.f_reverseBinary(substring(@s1,@TUVLength,1+((@tc-1)/8)))&power(2,@vc)<>0

begin

if@columntype<>'bit'

select@str1=@str1+@columnname+'=NULL,',@i=@i+@length

else

begin

select@str1=@str1+@columnname+'=NULL,'

set@bitAdd=casewhen@bitCount=0then@ielse@bitAddend

set@bitCount=(@bitCount+1)%8

set@i=@i+case@bitCountwhen1then1else0end

--

printrtrim(@bitAdd)+'->'+rtrim(@length)

end

end

elseif@columntype='char'

select@str1=@str1+@columnname+'='+convert(varchar(256),substring(@s1,@i,@length))+',',@i=@i+@length

elseif@columntype='nchar'

select@str1=@str1+@columnname+'='+convert(nvarchar(256),substring(@s1,@i,@length))+',',@i=@i+@length

elseif@columntype='datetime'

select@str1=@str1+@columnname+'='+convert(varchar,dateadd(second,convert(int,dbo.f_reverseBinary(substring(@s1,@i,4)))/300

,dateadd(day,convert(int,dbo.f_reverseBinary(substring(@s1,@i+4,4))),'1900-01-01')),120)+',',@i=@i+8

elseif@columntype='smalldatetime'

select@str1=@str1+@columnname+'='+convert(varchar,dateadd(minute,convert(int,dbo.f_reverseBinary(substring(@s1,@i,2)))

,dateadd(day,convert(int,dbo.f_reverseBinary(substring(@s1,@i+2,2))),'1900-01-01')),120)+',',@i=@i+4

elseif@columntype='int'

select@str1=@str1+@columnname+'='+rtrim(convert(int,dbo.f_reverseBinary(substring(@s1,@i,4))))+',',@i=@i+4

elseif@columntype='decimal'

select@str1=@str1+@columnname+'=DECIMAL,',@i=@i+@length

elseif@columntype='bit'

begin

set@bitAdd=casewhen@bitCount=0then@ielse@bitAddend

set@bitCount=(@bitCount+1)%8

select@str1=@str1+@columnname+'='+rtrim(convert(bit,substring(@s1,@bitAdd,1)&power(2,case@bitCountwhen0then8else@bitCountend-1)))+','

,@i=@i+case@bitCountwhen1then1else0end

--

printrtrim(@bitAdd)+'->'+rtrim(@length)

end

set@lib=@lib+1

end

set@i=convert(int,dbo.f_reverseBinary(substring(@s1,3,2)))+4+((@tc-1)/8)

set@lenVar=convert(int,dbo.f_reverseBinary(substring(@s1,@i,2)))

set@i=@i+2

set@ib=@i+@lenVar*2

set@ie=convert(int,dbo.f_reverseBinary(substring(@s1,@i,2)))

set@count=0

select@lib=min(pid),@lie=max(pid)from#twherep=2

while@lib<=@lie

begin

--

printrtrim(@ib)+'->'+rtrim(@ie)

select@columnname=name,@length=length,@columntype=typename,@vc=colid-1from#twherep=2andpid=@lib

ifdbo.f_reverseBinary(substring(@s1,@TUVLength,1+((@tc

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論