22

Is it possible to execute store procedure like a "table" for SELECT operator (MS SQL SERVER)?

Something like

SELECT TotalSum FROM exec MyStoreProcedure '2011/11/01', '2011/11/01'  

I mean somehow integrate it into the SELECT operator?

Thank you!


Thanks guys!

The solution what I did is based on your answers:

declare @result table (f1 varchar(20),f2 varchar(20), CodProducto int, NomProducto varchar(1000), Costo decimal, Cantidat int, Total decimal)
INSERT INTO @result exec  MyStoreProcedure '20111201', '20111201'
select * from @result
1
  • 1
    Nope, but you can insert those results on a table (if you know the columns of the result dataset of your sp, of course) Commented Mar 29, 2012 at 12:24

4 Answers 4

22

I supposed your proc returns several columns and you just want one, right?

small workaround is to add the result of the proc to a table variable and then select from it

create proc proc1 as
select 1 as one, 2 as two

declare @result table (one int, two int)

insert into @result
exec proc1

select one from @result
Sign up to request clarification or add additional context in comments.

1 Comment

Question: Is it possible to put it into SQLCommand object of .NET? Will do it work?
5

This would be better as a function rather than a stored procedure.

create function dbo.TestTable
(@var1 bit)
returns table
AS
RETURN
( select *
    from INFORMATION_SCHEMA.TABLES
    where @var1 = 1
);


select * from
dbo.TestTable(1)

Comments

2

Not directly (or without altering the stored procedure to be a table-valued function).

But you could do this:

INSERT INTO SomeTempTableWithSchemaMatchingTheSproc (...)
EXEC MyStoredProcedure 

SELECT * FROM SomeTempTableWithSchemaMatchingTheSproc 

SQL Server 2005 onwards, you can also use a table variable.

Comments

1

This works for me:

CREATE VIEW dbo.vw_xxx
AS
  select * from openquery(YOURSERVERNAME, 'exec [sp_xxx] '''','''','''','''','''','''' ')

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.