IF EXISTS
(
    SELECT 1
    FROM sys.objects
    WHERE
          object_id = OBJECT_ID(N'rds.sp_updatefrllorecipetype')
      AND type IN (N'P', N'PC')
)
    DROP PROCEDURE rds.sp_updatefrllorecipetype
GO
CREATE PROCEDURE rds.sp_updatefrllorecipetype(@ExportFrlloRecipeDataId UNIQUEIDENTIFIER, @WorkWithError bit)
AS
BEGIN

    declare @recid UNIQUEIDENTIFIER

    SELECT @recid = r.RecipeID
    FROM RDS.T_EXPORT_FRLLO_RECIPE_DATA D
    JOIN RDS.T_RECIPE r ON d.ObjId = r.RecipeID
    WHERE d.ExportFrlloRecipeDataID = @ExportFrlloRecipeDataId

    IF (@recid IS NOT NULL)
        BEGIN
            UPDATE R
            SET r.FrlloType = case WHEN @WorkWithError = 0 THEN (CASE WHEN r.FrlloType = 0 THEN null ELSE 0 END) 
                                            else r.FrlloType + 1
                                            end
            FROM RDS.T_RECIPE r
            WHERE r.RecipeID = @recid
        END
    ELSE
        BEGIN
            UPDATE r
            SET r.FrlloType = case WHEN @WorkWithError = 0 THEN (CASE WHEN r.FrlloType = 0 THEN null ELSE 0 END) 
                                            else r.FrlloType + 1
                                            end
            FROM rds.T_RECIPE_ELEMENT r
            JOIN RDS.T_EXPORT_FRLLO_RECIPE_DATA D ON r.RecipeElementID = d.ObjId
            WHERE d.ExportFrlloRecipeDataID = @ExportFrlloRecipeDataId
        END
END
GO
