如果我这样做,那不正确吗?
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
SELECT * FROM Inventory WHERE Warehouse = fn_GetWarehouse(@CustomerId)
理想情况下我想这样做,但ITVF不允许这样做:
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
SET @Warehouse = fn_GetWarehouse(@CustomerId)
SELECT * FROM Inventory WHERE Warehouse = @Warehouse
我不想使用MSTVF,因为查询优化器无法对其进行优化。最后我使用了这个解决方法:
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
SELECT * FROM fn_Inventory(fn_GetWarehouse(@CustomerId))
fn_Inventory接受@Warehouse作为参数,并使用参数查询Inventory表,从而避免此问题。处理这种情况是否没有标准模式?
答案 0 :(得分:1)
你忘记了回复陈述。
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
RETURN SELECT * FROM Inventory WHERE Warehouse = fn_GetWarehouse(@CustomerId)
或者,您需要begin和end来标记代码块,而不仅仅是单个return语句,所以:
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS @tableName TABLE (<TableDefinition>) AS
BEGIN
SET @Warehouse = fn_GetWarehouse(@CustomerId)
INSERT INTO @TableName SELECT * FROM Inventory WHERE Warehouse = @Warehouse
RETURN
END
答案 1 :(得分:0)
我会尝试制作fn_GetWarehouse
和内联表值函数并加入它。
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
RETURN (
SELECT
_Inventory.*
FROM Inventory _Inventory
join fn_GetWarehouse(@CustomerId) _Warehouse
on _Warehouse.warehouse = _Inventory.Warehouse
)