我想将变量\参数传递到视图中以按日期范围进行过滤。由于我在视图中进行分组,因此无法将 where 子句放在视图外部。
我已经阅读了很多关于这个的帖子,但还没有让它工作。我见过的将参数传递到视图中的大多数解决方法都涉及使用函数来返回值,这就是我卡住的地方。
/* My current view that I want to pass in start and end dates */
CREATE VIEW Total_Sales_By_Product_Num AS
SELECT products_all_fields.*,
Sum(<a bunch of code removed for this sample>) AS Item_Qty_Requested_Total,
Sum(<a bunch of code removed for this sample>) AS Item_Qty_Accepted_Total,
Sum(<a bunch of code removed for this sample>) AS Total_Sales_Dollars
FROM products_all_fields
LEFT OUTER JOIN (web_orders_items
INNER JOIN web_orders_header
ON web_orders_items.Web_Order_Num = web_orders_header.Web_Order_Num )
ON products_all_fields.Product_Num = web_orders_items.Product_Num
WHERE web_orders_header.Order_Date BETWEEN fn_GetStartDate() AND fn_GetEndDate()
GROUP BY products_all_fields.Product_Num
ORDER BY products_all_fields.Description
将参数返回给视图的函数:
CREATE FUNCTION fn_GetStartDate ()
RETURNS DATE
DETERMINISTIC NO SQL
BEGIN
RETURN @StartDate;
END
CREATE FUNCTION fn_GetEndDate ()
RETURNS DATE
DETERMINISTIC NO SQL
BEGIN
RETURN @EndDate;
END
这不起作用,它不返回任何数据,我知道我在做一些愚蠢的事情
set @fn_StartDate := '2012-01-01';
set @fn_EndDate := '2012-02-01';
select * from Total_Sales_By_Product_Num
谢谢你的帮助!