I have a dynamic SQL SP that its ‘Select’ statement stars thus: Select @sql = 'Declare @offer_date As datetime, @Id As bigint SELECT @offer_date = offe.offer_date, @Id = offe.Offer_id From Offers As offe ...
And I receive an error from app that says: You must declare the variable ‘@offer_date’. And I already declared it.. What is wrong?Here is the complete structure:USE market3GOALTER PROCEDURE findOf_dyn@CurrentPage int, @PageSize int, @Family smallint, @Product smallint, ..AsBEGIN SET NOCOUNT ON Declare @sql nvarchar(4000), @param_list nvarchar(4000), @Max_row int, @First_row int SET @Max_row = @PageSize * @CurrentPage SET @First_row = @Max_row - (@PageSize - 1) SET ROWCOUNT @First_row Select @sql = 'Declare @Offer_date As datetime, @Id As bigint SELECT @Offer_date = offe.Offer_date, @Id = offe.Offer_id From Offers As offe LEFT JOIN Products_name As prod On offe.Product_num = prod.Product_Id LEFT JOIN Families_product As fam On prod.Family_num = fam.Family_Id LEFT JOIN Offers_quality As of_qual On offe.Offer_id = of_qual.Offer_num LEFT JOIN Offers_rippening As of_rip On of_qual.Offer_qual_id = of_rip.Offer_qual_num WHERE 1 = 1' If @Family <> 0 SELECT @sql = @sql + ' AND (fam.Family_Id = @xFamily Or (x@Family = 9 AND offe.Other_families IS NOT NULL))' If @Product <> 0 SELECT @sql = @sql + ' AND offe.Product_num = @xProduct' SELECT @sql = @sql + ' Order by offe.Offer_date Desc, offe.Offer_id Desc' SELECT @param_list = ' @xCurrentPage int, @xPageSize int, @xFamily smallint, @xProduct smallint, ...' EXEC sp_executesql @sql, @param_list, @CurrentPage, @PageSize, @Family, @Product, ......
What I have to do to SELECT @Offer_date = offe.Offer_date, @Id = offe.Offer_id inside ‘@sql’? I need these variables (@Offer_date and @Id) to use it later in the SPThanks