Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a stored procedure inside a select query

SELECT col1,        col2,        col3,  EXEC GetAIntFromStoredProc(T.col1) AS col4      FROM Tbl AS T      WHERE (col2 = @parm)  

How to write this SQL query in SQL Server 2008?

like image 938
Joakim Avatar asked Jan 24 '13 17:01

Joakim


People also ask

Can you execute a stored procedure in a select statement?

We can not directly use stored procedures in a SELECT statement.

How do you use a stored procedure in a query?

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. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

How do I write a stored procedure for select query in SQL Server?

How to create a SELECT stored procedure? Click on your Database and expand “Programmability” item and right click on “Stored Procedures” or press CTRL + N to get new query window. In the query area between BEGIN and END, type your SELECT statement to select records from the table.

How do I select a stored procedure in SQL?

Expand Stored Procedures, right-click the procedure and then select Script Stored Procedure as, and then select one of the following: Create To, Alter To, or Drop and Create To. Select New Query Editor Window. This will display the procedure definition.


Video Answer


1 Answers

Thanks @twoleggedhorse.

Here is the solution.

  1. First we created a function

    CREATE FUNCTION GetAIntFromStoredProc(@parm Nvarchar(50)) RETURNS INTEGER  AS BEGIN    DECLARE @id INTEGER     set @id= (select TOP(1) id From tbl where col=@parm)     RETURN @id END 
  2. then we do the select query

    Select col1, col2, col3, GetAIntFromStoredProc(T.col1) As col4 From Tbl as T Where col2=@parm 
like image 67
Joakim Avatar answered Sep 27 '22 22:09

Joakim