I use dynamic sql for read products in my project. I want with the number for statment create sql statment my code is :
CREATE PROCEDURE dbo.Asbabbazi_A
@name_product nvarchar(50),
@first_price int,
@final_price int,
@max_registered_price int,
@collection_1 nvarchar(30),
@id_state tinyint,
@first smallint,
@final smallint AS
begin
DECLARE @SQLstring nvarchar(1000)
DECLARE @PARAMS nvarchar(1000);
set @SQLstring = 'SELECT IDproduct,name_product,first_price,final_price,max_registered_price,date_record_shamsi,final_date_view_shamsi,
count_views,image_1,collection_1 from Table_asbabbazi where active=0'
if(@name_product != "no name")
@SQLstring = @SQLstring + 'AND (name_product LIKE '%'+(@name_product)+'%')'
if (@finalPrice != 0)
@SQLstring = @SQLstring + 'AND ( first_price between @first_price AND @final_price )'
if (subCollection != "انتخاب کنید")
@SQLstring = @SQLstring + 'AND (collection_1=@collection_1 )'
if (state != 0)
@SQLstring = @SQLstring + 'AND (id_state=@id_state )'
set @PARAMS =' @name_product nvarchar(50),
@first_price int,
@final_price int,
@max_registered_price int,
@collection_1 nvarchar(30),
@id_state tinyint,
@first smallint,
@final smallint '
Execute sp_Executesql @SQLstring,
@name_product ,
@first_price ,
@final_price ,
@max_registered_price ,
@collection_1 ,
@id_state ,
@first ,
@final
end
RETURN
Error message content is:
Incorrect syntax near '@SQLstring'.
Must declare the scalar variable "@finalPrice".
Incorrect syntax near '@SQLstring'.
Incorrect syntax near '@SQLstring'.
It dont work and show error message please help