Apply multiple documents, each document multiple details, to the details of each document, business processing. Circular data, identifying the first detail of each document.
--CREATE TABLE ABC--(--A int,--B int,--C int--)--Insert ABC Select ' 1 ', ' a ', null--Insert ABC Select ' 1 ', ' ten ', null--Insert ABC Select ' 2 ', ' + ', null--Insert ABC Select ' 2 ', ' 5 ', nullif exists(Select * fromsysobjectswhereId=object_id(N'tempdb: #ttt') ) Drop Table#tttDECLARE @sid INT,@sno INT,@preid int,@curid int,@ccid int,@ctotal intSelect IDENTITY(int,1,1) asFid* into#ttt fromAbcOrder byAASCBdescSET @sno=@ @rowcountSET @sid=1Set @preid=0Set @ctotal=0 while @sid<=@snoBEGINSelect @curid=A from#tttwhereFid=@sidSelect @ccid=B from#tttwhereFid=@sidif @preid<>@curidbeginSet @ctotal=@ccidSet @preid=@curidEndElsebeginSet @ctotal=@ctotal-@ccidEndUpdate#tttSetC=@ctotal whereFid=@sidSET @sid=@sid+1ENDSelect * from#tttDrop Table#ttt
Fid |
A |
B |
C |
1 |
1 |
20 |
20 |
2 |
1 |
10 |
10 |
3 |
2 |
15 |
15 |
4 |
2 |
5 |
10 |
Multiple documents, each document a number of details, cycle, to determine the beginning of each document