1
I am trying to set the variable with a query, select only works this way using WITH, but it is giving error when I will assign the value in the variable.
The code I’m trying to:
DECLARE @contador INT, @empresas INT, @id_empresa INT
SET @contador = 1
SET @empresas = (SELECT COUNT(ID_Pessoa) FROM Empresa)
WHILE (@contador <= @empresas)
BEGIN
SET @id_empresa = with CTE_R as
(SELECT e.ID_Pessoa, ROW_NUMBER() OVER(ORDER BY ID_Pessoa) as RowNum FROM Empresa e (NOLOCK))
select ID_Pessoa from CTE_R where RowNum = 1
INSERT INTO Produto_Lista_Empresa (ID_Produto_Lista, ID_Empresa)
SELECT ID, @id_empresa FROM Produto_Lista
SET @contador = @contador + 1
CONTINUE;
END
It is giving error when assigning the value of the variable @id_company
Where is giving the error and which error appears?
– Ronaldo Araújo Alves
is giving error when assigning value to @id_company variable, error is: "Incorrect syntax near the keyword 'with'."
– Gabriel Henrique