views:

4235

answers:

8

How would I call a Stored Procedure that returns data in a View? Is this even possible?

+2  A: 

You would have to script the View like below. You would essentially write the results of your proc to a table var or temp table, then select into the view.

Edit - If you can change your stored procedure to a Table Value function, it would eliminate the step of selecting to a temp table.

**Edit 2 ** - Comments are correct that a sproc cannot be read into a view like I suggested. Instead, convert your proc to a table-value function as mentioned in other posts and select from that:

create view sampleView
as select field1, field2, ... 
from dbo.MyTableValueFunction

I apologize for the confusion

Rob Allen
I'm pretty sure you cannot do this in SQL Server.
Cade Roux
Actually, I'm certain you can't do this, because views aren't allowed to have BEGIN END blocks in the first place.
Cade Roux
You are correct... investingating. I know I have populated views before in a similar manner.
Rob Allen
updated to reflect [closer to] reality
Rob Allen
I don't want to imply that you can't populate a table in another step and have a view off that table. But nothing to trigger that population to happen automatically. There's also the OPENROWSET trick, but that has other drawbacks.
Cade Roux
@Cade Roux - indeed the openrowset is a hackish solution. You could populate a #temp table and in the same connection consume it in a view - but thats just asking for trouble.
Rob Allen
+1  A: 

No, that's not possible.

The closest thing would be calling a function from the view, which may or may not be able to do what you want?

JerSchneid
+1  A: 

If you are using Sql Server 2005 you can use table valued functions. You can call these directly and pass paramters, whilst treating them as if they were tables.

For more info check out http://msdn.microsoft.com/en-us/library/ms191165.aspx

Macros
It appears that table valued functions are available in SQL Server 2000 as well:http://www.devarticles.com/c/a/SQL-Server/Creating-User-Defined-Functions-In-SQL-Server-2000/4/
polara
A: 

There is no way to do it. The standard doesn't support it. You should consider to re-design your schema, to avoid this issue.

Artem Barger
A: 

Not possible. What is the problem that you are solving?

AlexKuznetsov
+3  A: 

This construction is not allowed in SQL Server. An inline table-valued function can perform as a parameterized view, but is still not allowed to call an SP like this.

Here's some examples of using an SP and an inline TVF interchangeably - you'll see that the TVF is more flexible (it's basically more like a view than a function), so where an inline TVF can be used, they can be more re-eusable:

CREATE TABLE dbo.so916784 (
    num int
)
GO

INSERT INTO dbo.so916784 VALUES (0)
INSERT INTO dbo.so916784 VALUES (1)
INSERT INTO dbo.so916784 VALUES (2)
INSERT INTO dbo.so916784 VALUES (3)
INSERT INTO dbo.so916784 VALUES (4)
INSERT INTO dbo.so916784 VALUES (5)
INSERT INTO dbo.so916784 VALUES (6)
INSERT INTO dbo.so916784 VALUES (7)
INSERT INTO dbo.so916784 VALUES (8)
INSERT INTO dbo.so916784 VALUES (9)
GO

CREATE PROCEDURE dbo.usp_so916784 @mod AS int
AS 
BEGIN
    SELECT  *
    FROM    dbo.so916784
    WHERE   num % @mod = 0
END
GO

CREATE FUNCTION dbo.tvf_so916784 (@mod AS int)
RETURNS TABLE
    AS
RETURN
    (
     SELECT *
     FROM   dbo.so916784
     WHERE  num % @mod = 0
    )
GO    

EXEC dbo.usp_so916784 3
EXEC dbo.usp_so916784 4

SELECT * FROM dbo.tvf_so916784(3)    
SELECT * FROM dbo.tvf_so916784(4)

DROP FUNCTION dbo.tvf_so916784
DROP PROCEDURE dbo.usp_so916784
DROP TABLE dbo.so916784
Cade Roux
A: 

"create view sampleView as select field1, field2, ... from dbo.MyTableValueFunction"

Note that even if your MyTableValueFunction doesn't accept any parameters, you still need to include parentheses after it, i.e.:

... from dbo.MyTableValueFunction()

Without the parentheses, you'll get an "Invalid object name" error.

A: 
exec sp_addlinkedserver 
        @server = 'local',
        @srvproduct = '',
        @provider='SQLNCLI',
        @datasrc = @@SERVERNAME
go

create view ViewTest
as
select * from openquery(local, 'sp_who')
go

select * from ViewTest
go
Glen