|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
有了数据以后,我们就要想一个比较统一的方法来闪回。上面我们说了对于DML操作,可以通过反向执行所有逆操作来实现,对于语句里面的DDL,只能直接跳过。原因是一个DDL不一定有直接的逆操作。server|sqlserver
1.分批取
declare@P1int
set@P1=180150000
declare@P2int
set@P2=8
declare@P3int
set@P3=1
declare@P4int
set@P4=3
execsp_cursoropen@P1output,
Nselecttop3*fromauthors,
@P2output,
@P3output,
@P4output
select@P1,@P2,@P3,@P4
go
execsp_cursorfetch180150000,16,1,1
go
execsp_cursorfetch180150000,16,2,1
go
execsp_cursorfetch180150000,16,3,1
go
execsp_cursorfetch180150000,16,4,1
go
execsp_cursorclose180150000
go
execsp_cursorfetch180150000,16,1,10--第1P起,取10P
execsp_cursorclose180150000
go
2.取相PID的
Thetable-valuedfunctionfn_FindReports(InEmpID),which--givenanEmployeeID--returnsatablecorrespondingtoalltheemployeesthatreporttothegivenemployeedirectlyorindirectly.Thislogicisnotexpressibleinasinglequeryandisagoodcandidateforimplementingasauser-definedfunction.
CREATEFUNCTIONfn_FindReports(@InEmpIdnchar(5))
RETURNS@retFindReportsTABLE(empidnchar(5)primarykey,
empnamenvarchar(50)NOTNULL,
mgridnchar(5),
titlenvarchar(30))
/*Returnsaresultsetthatlistsalltheemployeeswhoreporttogiven
employeedirectlyorindirectly.*/
AS
BEGIN
DECLARE@RowsAddedint
--tablevariabletoholdaccumulatedresults
DECLARE@reportsTABLE(empidnchar(5)primarykey,
empnamenvarchar(50)NOTNULL,
mgridnchar(5),
titlenvarchar(30),
processedtinyintdefault0)
--initialize@Reportswithdirectreportsofthegivenemployee
INSERT@reports
SELECTempid,empname,mgrid,title,0
FROMemployees
WHEREempid=@InEmpId
SET@RowsAdded=@@rowcount
--Whilenewemployeeswereaddedinthepreviousiteration
WHILE@RowsAdded>0
BEGIN
/*Markallemployeerecordswhosedirectreportsaregoingtobe
foundinthisiterationwithprocessed=1.*/
UPDATE@reports
SETprocessed=1
WHEREprocessed=0
--Insertemployeeswhoreporttoemployeesmarked1.
INSERT@reports
SELECTe.empid,e.empname,e.mgrid,e.title,0
FROMemployeese,@reportsr
WHEREe.mgrid=r.empidande.mgride.empidandr.processed=1
SET@RowsAdded=@@rowcount
/*Markallemployeerecordswhosedirectreportshavebeenfound
inthisiteration.*/
UPDATE@reports
SETprocessed=2
WHEREprocessed=1
END
--copytotheresultofthefunctiontherequiredcolumns
INSERT@retFindReports
SELECTempid,empname,mgrid,title
FROM@reports
RETURN
END
GO
对于update操作,event中依次记录旧行,新行的值。 |
|