In SQL Server, if I have a scalar-value function that takes a table-valued parameter argument, defined as follows (simplified example):
CREATE TYPE IntTableType AS TABLE(Value INT);
CREATE FUNCTION dbo.MeetsCustomRequirements
(
@TypeIDs IntTableType READONLY
)
RETURNS TINYINT AS
BEGIN
-- (Function logic here; returns either 0 or 1 based on the specified @TypeIDs values)
END
I'd like to be able to call that function from a view, where the table-valued parameter of the view is generated from an inline query, like:
CREATE VIEW dbo.vEligibleItems AS
SELECT i.ItemID
FROM tItems i
WHERE dbo.MeetsCustomRequirements (
(SELECT TypeID FROM tItemTypes WHERE ItemID = i.ItemID)
);
However, this doesn't work because the result of that nested SELECT query isn't an IntTableType (even though it is a list of INT values). (Specific error generated is Error 206: Operand type clash: int is incompatible with IntTableType.)
My two-part question: Is there a way to call a function taking a table-valued parameter argument from a view? If not, what alternative approaches are available to accomplish the same thing?
Database is SQL Server 2008.
(Bringing the logic in the function inline in the view is not ideal because the function is called from several different places, not just from this one view.)
dbo.MeetsCustomRequirements?