【发布时间】:2015-02-13 14:38:35
【问题描述】:
我在我的项目中使用动态 sql 来读取产品。 我想用 statment 的编号 create sql statment 我的代码是:
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
错误信息内容为:
“@SQLstring”附近的语法不正确。
必须声明标量变量“@finalPrice”。
“@SQLstring”附近的语法不正确。
“@SQLstring”附近的语法不正确。
它不工作并显示错误消息 请帮忙
【问题讨论】:
-
你的帖子看起来一团糟——清理一下。如果您无法向我们提供任何错误消息,请尽量减少您的查询,即删除一小部分查询并重试等,直到您发现问题为止。
-
if (subCollection != "انتخاب کنید") 和 if(@name_product != "no name"),为什么这有双引号而不是单引号?还有很多其他的事情需要修复。你有没有检查过这个?我也看不到你在@SQLString 之前使用 SET。你应该看看 MSSQL 语言语法。
标签: sql stored-procedures dynamic-sql