0

I have a query with dynamic variables that I arm or execute by means of an EXECUTE sp_executesql statement with output to a variable by means of the output clause but for some reason it enter code heres not possible that the result is store antifreeze in that variable, only NULL comes out.I only speak Spanish so apologies if I make mistakes when translating the question

DECLARE @contador2 INT;
DECLARE @Num_reg2 INT;
DECLARE @Variable2 VARCHAR(55);
DECLARE @Parametro INT;
DECLARE @SQLString nvarchar(500);  
DECLARE @ParmDefinition nvarchar(500);  
SET @contador   = 0;
SET @contador2= 1;
SET @Num_reg2=(SELECT COUNT(Concatenada) FROM [ConsultaIndicadores].[dbo].[Fechas]);
WHILE @contador2 <= @Num_reg2
BEGIN
SET @SQLString =  N'SELECT Concatenada FROM [ConsultaIndicadores].[dbo].[Fechas] WHERE RegNumero = @ID';  
SET @ParmDefinition = N'@ID int, @Variable varchar(55) OUTPUT';
SET @contador = @contador + 1; 
EXECUTE sp_executesql @SQLString, @ParmDefinition, @ID = @contador, @Variable=@Variable2 OUTPUT;
SELECT @Variable2;
SET @contador2 = @contador + 1;
END;

The problem is that the result of the query if it was done directly to the SELECT appears correct but the variable @Variable2 appears NULL

0 Answers0