执行动态sql时出现'错误'附近的语法错误

时间:2015-02-13 14:38:36

标签: sql stored-procedures dynamic-sql

我在项目中使用动态sql读取产品。 我想用statment的数字创建sql语句 我的代码是:

 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'附近的语法不正确。

它不起作用并显示错误消息 请帮忙

4 个答案:

答案 0 :(得分:0)

在其他可能的错误中,您需要在所有字符串的开头或结尾(或两者)中添加空格,以确保它们不会发生冲突。

正如这一块:

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)+'%')'

会在第一个字符串的末尾碰撞active=0,在下一个字符串的开头碰撞AND

... active=0AND ...

答案 1 :(得分:0)

我相信你一定要检查MSSQL的SQL语法。你有几个错误。首先,为什么你在下面使用双引号?

if (subCollection != "انتخاب کنید") 

if(@name_product != "no name")

其次,为什么在@SQLString之前没有设置SET?

尝试以下方法;

 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')
 SET @SQLstring = @SQLstring + 'AND (name_product  LIKE '%'+(@name_product)+'%')'
 if (@final_price != 0)
 SET @SQLstring = @SQLstring +  'AND ( first_price between  @first_price AND @final_price )'
 if (subCollection != 'انتخاب کنید')
 SET @SQLstring = @SQLstring + 'AND (collection_1=@collection_1  )'
 if ([state] != 0)
 SET @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

你一定要读懂SQL。

另外,请参阅单引号和双引号及其用法;

What is the difference between single and double quotes in SQL?

答案 2 :(得分:0)

您可以从语法突出显示存在问题。例如,在这一行:

@SQLstring = @SQLstring + 'AND (name_product  LIKE '%'+(@name_product)+'%')'

'%'字符不是任何字符串的一部分。您需要额外的单引号:

@SQLstring = @SQLstring + 'AND (name_product  LIKE ''%''' + @name_product + '''%'')'

这可能是唯一的问题。或不。如果没有,我建议您尽可能简单地select <col> from <table>开始,并逐步添加到代码中以逐一发现问题。引用动态SQL可能很棘手。

答案 3 :(得分:0)

我更改了我的代码,现在可以使用了:

CREATE PROCEDURE  dbo.Asbabbazi_A
    (@name_product nvarchar(50),
    @first_price int,
    @final_price int,
    @collection_1 nvarchar(30),
    @id_state tinyint)
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')
set @SQLstring = @SQLstring + ' AND (name_product  LIKE %@name_product%)'
if (@final_price != 0)
set @SQLstring = @SQLstring +  ' AND ( first_price between  @first_price AND @final_price )'
if (@collection_1 != 'انتخاب کنید')
set @SQLstring = @SQLstring + ' AND (collection_1=@collection_1  )'
if (@id_state != 0)
set @SQLstring = @SQLstring + ' AND (id_state=@id_state  )'

execute @SQLstring
END