Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a Stored procedure in SQL CTE

Are you allowed to exec stored procedures within a SQL CTE statement? I'm a bit new to sql cte queries...

like image 894
user167698 Avatar asked Jun 13 '11 15:06

user167698


People also ask

Can we call stored procedure in CTE?

According to the CTE documentation, Common Table Expression is a temporary result set or a table in which we can do CREATE, UPDATE, DELETE but only within that scope. That is, if we create the CTE in a Stored Procedure, we can't use it in another Stored Procedure.

Can you call a stored procedure?

You can call an SQL stored procedure with the execute, open, or get statement; in each case, you use the #sql directive. A stored procedure is a set of instructions for a database, like a function in EGL.

How do you call CTE in SQL?

Using the CTE –After you define your WITH clause with the CTEs, you can then reference the CTEs as you would refer any other table. However, you can refer a CTE only within the execution scope of the statement that immediately follows the WITH clause.

How do I execute a stored procedure in SQL Server?

In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases. Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure.


2 Answers

No, sorry. SELECTs statments only

If you need to use stored proc output (result set), then it'd be a temp table

CREATE TABLE #foo (bar int...)

INSERT #foo (bar, ...)
EXEC myStoredProc @param1...

-- more code using #foo
like image 97
gbn Avatar answered Oct 17 '22 19:10

gbn


You can also use table variable :

DECLARE @tbl TABLE(id int ,name varchar(500) ,...)      
    INSERT INTO @tbl        
    EXEC myprocedure @param ..

with cte as (
    SELECT * FROM @tbl  
)
select * from cte
like image 4
Sagar Dev Timilsina Avatar answered Oct 17 '22 19:10

Sagar Dev Timilsina