【问题标题】:Incorrect syntax near' error while executing dynamic sql执行动态 sql 时出现错误语法附近错误
【发布时间】: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


【解决方案1】:

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

就目前的情况而言:

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 ...

【讨论】:

    【解决方案2】:

    你可以从语法高亮中看出有问题。例如,在这一行:

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

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

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

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

    【讨论】:

      【解决方案3】:

      我相信您绝对应该检查 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?

      【讨论】:

      • 说明双引号用于标识符(表名、列名等),单引号用于字符串。
      • @jarlh 我认为这是众所周知的。在 SQL 中编写存储过程是一个 300 级的类。但会做:)
      【解决方案4】:

      我更改了我的代码,它现在可以工作了:

      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
      

      【讨论】:

        猜你喜欢
        • 2020-01-02
        • 1970-01-01
        • 2013-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多