0

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

select value from db_function(passing some parameters..)
4
  • why can't you try it? Commented Dec 14, 2016 at 17:07
  • I tried, i am getting Msg 208, Level 16, State 3, Line 39 Invalid object name 'dbo.SP_GET_INFO'. Commented Dec 14, 2016 at 17:10
  • 1
    Then your function doesn't exist, or isn't named that... Commented Dec 14, 2016 at 17:14
  • 1
    @user648026 The name SP_GET_INFO points to a stored procedure (the SP_... - btw: One should not use the prefix sp_, this is reserved! ). The result of a SP is not so easy to retrieve. The only way is INSERT INTO SomeTableWithFittingStructure EXEC dbo.SP_GET_INFO... Commented Dec 14, 2016 at 17:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.