MySql存储过程未编译

时间:2015-06-07 19:32:34

标签: mysql sql mysqli mysql-workbench mysql-error-1064

DELIMITER //

CREATE PROCEDURE SearchProduct(

    IN Search VARCHAR(1000),
    IN OrderBy Varchar(100),
    In FromId varchar(10),
    In ToId varchar(10)

    )

BEGIN

Set @MQuery= '
 with DataTable as (

    SELECT Product.Id as ProductId, 
           Category.Id as CategoryId,
           Product.Active,
           Product.Name as ProductName,
           Catgeory.Name as CategoryName,
           Product.AddedOn,
           Product.AddedBy as UserId, 
           ROW_NUMBER() OVER (Order by '+ OrderBy  +') RowNumber,
    Count(*) OVER() as TotalRecords 

    FROM Product
    inner join ProductCategory 
    on Product.Id=ProductCategory.ProductId
    inner join Category 
    on Category.Id= ProductCategory.CategoryId

WHERE ' + Search+ ' ) '+

'
Select DataTable.*, (User.FirstName+space(1) + User.LastName) as FullName

from DataTable 
inner join User on DataTable.UserId= User.Id
where DataTable.RownNumber berween '+FromId +' and '+ ToId
+ ' order by DataTable.RowNumber'

execute @MQuery;

 END //

DELIMITER ;

导致以下错误:

  

#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在' execute @MQuery附近使用正确的语法;

     

END'在第17行

1 个答案:

答案 0 :(得分:0)

在MySQL中连接字符串的正确方法是使用函数concat()。执行它们的正确方法是使用prepare / exec

而且,MySQL中没有with语句。你试图写的陈述是这样的:

Set @MQuery = concat('
 with DataTable as (

    SELECT Product.Id as ProductId, 
           Category.Id as CategoryId,
           Product.Active,
           Product.Name as ProductName,
           Catgeory.Name as CategoryName,
           Product.AddedOn,
           Product.AddedBy as UserId, 
           ROW_NUMBER() OVER (Order by '+ OrderBy  +') RowNumber,
    Count(*) OVER() as TotalRecords 

    FROM Product
    inner join ProductCategory 
    on Product.Id=ProductCategory.ProductId
    inner join Category 
    on Category.Id= ProductCategory.CategoryId

WHERE ', Search, ' ) ', 
'
Select DataTable.*, concat(User.FirstName, ' ', User.LastName) as FullName

from DataTable 
inner join User on DataTable.UserId= User.Id
where DataTable.RownNumber berween ', FromId, ' and ', ToId,
' order by DataTable.RowNumber' );

prepare stmt from @MQuery;

exec stmt;

这可能会编译,但你会遇到其他问题。您需要重新构建MySQL的查询。显然,在编写存储过程之前,您没有尝试先运行查询。这就像去赌博并把所有的钱都放在你玩的第一场比赛上。它可能有用,但不太可能。

看起来你正在混合使用MySQL和SQL Server语法。