If I do this, it is not good correct?
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
SELECT * FROM Inventory WHERE Warehouse = fn_GetWarehouse(@CustomerId)
Ideally I want to do this, but this is not allowed for ITVF:
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
SET @Warehouse = fn_GetWarehouse(@CustomerId)
SELECT * FROM Inventory WHERE Warehouse = @Warehouse
I don't want to use MSTVF because it cannot be optimized by the query optimizer. In the end I use this work-around:
ALTER FUNCTION [dbo].[fn_ItemInventory] (@CustomerId)
RETURNS TABLE AS
SELECT * FROM fn_Inventory(fn_GetWarehouse(@CustomerId))
The fn_Inventory accepts @Warehouse as parameter and queries Inventory table using the parameter, thus avoid the issue. Is there no standard pattern for handling this situation?
fn_Inventory?... WHERE Warehouse = dbo.fn_GetWarehouse(@CustomerId)