Patrick Desjardins Patrick Desjardins - 5 months ago 8
SQL Question

How to query from a stored procedure in SQL Server?

Let say I have a simple Stored Procedure:

ALTER PROCEDURE [dbo].[myProc]
AS
BEGIN
SELECT * FROM myTable
END


How can I do a WHERE statement in Microsoft SQL Server Management Studio to the stored procedure? Something like that:

SELECT * FROM myProc WHERE x = 'a'; -- But that doesn't work...

Answer

It sounds like you're trying to make a "dynamic" stored procedure.

Something you might want to do is:

1) Insert the contents of your stored procedure into a temporary table

2) Use dynamic sql to apply a where condition to that temporary table.

Something like:

declare @as_condition varchar(500); --Your condition

create table #a
(
id bigint
)

insert into #a
execute sproc

declare @ls_sql varchar(max);
set @ls_sql = "select * from #a where " + @as_condition;
execute (@ls_sql);
Comments