DECLARE @TEMP TABLE
(
Id INT,
LawName NVARCHAR(10),
LogName VARCHAR(10),
countid int
)
DECLARE @Id INT;
DECLARE @LawName VARCHAR(50);
DECLARE @LogName NVARCHAR(10);
DECLARE MYCUR CURSOR FOR
(SELECT Id,LawName,LogName FROM Lawer)--聲明遊標
OPEN MYCUR--開啟遊標
FETCH NEXT FROM MYCUR INTO @Id,@LawName,@LogName
WHILE @@FETCH_STATUS = 0--當SQLCODE值為0時表明一切正常,100表示已經取到了結果集的末尾
BEGIN
DECLARE @CNT INT;
SELECT @CNT=count(Id) FROM Lawer WHERE Id=@Id AND LawName=@LawName AND LogName=@LogName
INSERT INTO @TEMP(Id,LawName,LogName,countid) VALUES(@Id,@LawName,@LogName,@CNT)
FETCH NEXT FROM MYCUR INTO @Id,@LawName,@LogName
END
CLOSE MYCUR--關閉遊標
DEALLOCATE MYCUR--刪除遊標資源
SELECT * FROM @TEMP
/*--定位到指定位置的記錄
fetch absolute 56488 from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到目前記錄相對位置記錄
fetch relative -88 from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到目前記錄前一條
fetch prior from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到目前記錄後一條
fetch next from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到首記錄
fetch first from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到尾記錄
fetch last from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
*/