Is the Condition table a temporary table? The temporary table in the loop should be created in advance.
Then output variables to troubleshoot errors.Like this:
DECLARE @Code VARCHAR(20)
DECLARE @ZPID INT
DECLARE @Sql nvarchar(max)
DECLARE @Con nvarchar(max)
DECLARE @ConStr nvarchar(max)
WHILE (Select Count(*) From Codes with(nolock) where Proceed =0 ) > 0
BEGIN
SELECT Top 1 @ZPID=ZPLID, @Code=Code From Codes with(nolock) where Proceed=0
INSERT INTO #Condition
SELECT *
FROM Gen G with(nolock)
WHERE g.Code=@Code AND G.ZPLID=@ZPID AND G.ZfeatureKey IS NOT NULL
SET @Con= STUFF((SELECT CONCAT(' Or (PM.ZfeatureKey= ', CC.ZfeatureKey
, ' And AcceptedValuesOption_Value '
, CAST(EStrat AS NVARCHAR(2500))
, IIF(EEnd='',''
,CONCAT(' And AcceptedValuesOption_Value ',EEnd)),')')
FROM #Condition CC
INNER JOIN Allfeatures AL with(nolock)
ON AL.ZfeatureKey = CC.ZfeatureKey AND AL.IsNumericValues >0
FOR XML PATH(''), TYPE).value('(./text())[1]','varchar(max)'),1,3,'')
PRINT @Con
END
Regards
Echo