0
Good night,
I am making a cursor that updates the table level column of schools with the following rules: a. Schools with more than three levels of education are graded with category A b. Schools with three levels of education are graded with category B c. Schools with two levels of education are graded with category C d. Other schools are graded with category D
The cursor is placing the result of the Level Column as category 'C' on all lines, I want to take the result of my SELECT and use it to add the categories in the Level column, follow the code so far:
-- Variáveis do Cursor
DECLARE @Nivel CHAR(1),
@CodigoNivelEnsino INT,
@Contador INT= 0;
-- Cursor que atualiza a coluna nível da tabela de escolas.
DECLARE cur_AttTbNivel CURSOR FOR
SELECT CodigoNivelEnsino,count(CodigoNivelEnsino) AS 'Quantidade de Escolas por Nivel de Ensino'
FROM Escola_NivelEnsino
GROUP BY CodigoNivelEnsino
HAVING COUNT(CodigoNivelEnsino) > 1
ORDER BY COUNT(CodigoNivelEnsino) DESC
SELECT Nivel
FROM Escola
--Abrindo o cursor
OPEN cur_AttTbNivel;
-- Selecionar dados
FETCH NEXT FROM cur_AttTbNivel
INTO @Nivel, @CodigoNivelEnsino
WHILE @@FETCH_STATUS = 0
BEGIN
IF @CodigoNivelEnsino >3
UPDATE Escola
SET Nivel = 'A'
IF @CodigoNivelEnsino = 3
UPDATE Escola
SET Nivel = 'B'
IF @CodigoNivelEnsino = 2
UPDATE Escola
SET Nivel = 'C'
ELSE
UPDATE Escola
SET Nivel = 'D'
WHERE
Nivel = @Nivel
FETCH NEXT FROM cur_AttTbNivel INTO @Nivel, @CodigoNivelEnsino;
END
-- Fechando e desalocando o cursor da memória
CLOSE cur_AttTbNivel
DEALLOCATE cur_AttTbNivel
Data from the Tables in question: