JavaSheriff
JavaSheriff

Reputation: 7665

mssql Is it possible to query db function?

Is it possible to create a db view that will query a db function?

select value from db_function(passing some parameters..)

Upvotes: 0

Views: 27

Answers (1)

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67291

Creating an (inline!) table valued function is easy. Check it out:

CREATE FUNCTION dbo.TestFunction(@StartOfName VARCHAR(100))
RETURNS TABLE
AS
RETURN
SELECT * FROM sys.objects AS o WHERE o.name LIKE @StartOfName + '%';
GO
SELECT * FROM dbo.TestFunction('m');
GO
DROP FUNCTION dbo.TestFunction;

This will return all objects, where the name starts with 'm'.

Such a TVF can be used like a table, can be joined into a select with APPLY.

The VIEW you want to create can use this function quite as easily as any other VIEW or physical table.

Upvotes: 2

Related Questions