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 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'execute @MQuery;
END' at line 17
在DataTable.RowNumber命令後面可能需要一個';'分隔符來終止'SET'語句。 –
你正在使用CTE,所以它不會在MySQL上工作。也許你的意思是SQL服務器? – Mihai
確定..其實我一般在Sql服務器上使用這個存儲過程,但是現在我在mysql上工作,所以需要在Mysql – Jimmy