Criando um cursor simples no SQL Server
Bom para quem ainda tem alguma dúvida sobre como criar um cursor, segue uma pequena dica sobre as tabelas do grupo TOTVS em especial o segmento RM
Caso tenham gostado, comente e compartilhe. Obrigado!
DECLARE @TOTAL INT, @CGCCFO VARCHAR(25)
DECLARE CurCODCFO CURSOR
KEYSET
FOR (SELECT COUNT(*) TOTAL, CGCCFO FROM FCFO WHERE CGCCFO IS NOT NULL GROUP BY CGCCFO HAVING COUNT(*) > 1)
OPEN CurCODCFO
FETCH FIRST FROM CurCODCFO INTO @TOTAL, @CGCCFO
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT CODCFO, CGCCFO, CODCOLIGADA FROM FCFO WHERE CGCCFO = @CGCCFO
FETCH NEXT FROM CurCODCFO INTO @TOTAL, @CGCCFO
END
CLOSE CurCODCFO
DEALLOCATE CurCODCFO
DECLARE CurCODCFO CURSOR
KEYSET
FOR (SELECT COUNT(*) TOTAL, CGCCFO FROM FCFO WHERE CGCCFO IS NOT NULL GROUP BY CGCCFO HAVING COUNT(*) > 1)
OPEN CurCODCFO
FETCH FIRST FROM CurCODCFO INTO @TOTAL, @CGCCFO
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT CODCFO, CGCCFO, CODCOLIGADA FROM FCFO WHERE CGCCFO = @CGCCFO
FETCH NEXT FROM CurCODCFO INTO @TOTAL, @CGCCFO
END
CLOSE CurCODCFO
DEALLOCATE CurCODCFO
Caso tenham gostado, comente e compartilhe. Obrigado!
Nenhum comentário