Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL How to run a procedure/function within a SELECT query?

Tags:

tsql

I can't check it right now (don't have a compiler right now), but will that query executes ?

select myTable.id_customer, [my_procedure_or_function](myTable.id_customer)

from myTable

group by myTable.id_customer

that procedure/function returns a NUMERIC(18,0) or NULL

to sum up, I need to select distinct id_customer from that table and for that id - get a number/null value currently associated with that id_customer

like image 225
Tony Avatar asked Oct 11 '10 14:10

Tony


People also ask

Can we call procedure inside SELECT query?

You cannot call a procedure in a select statement, because it does not return anything.

Can you execute a stored procedure in a SELECT statement?

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

Can we call procedure inside function in SQL?

You can call a stored procedure inside a user-defined function. Consider this example: SQL> create table test_tab (tab_id number); Table created.


1 Answers

Syntactically it will work for a scalar UDF but not a stored procedure.

select myTable.id_customer, mySchema.myFunction(myTable.id_customer) As myAlias
from myTable
group by myTable.id_customer

However dependant upon what the scalar UDF is doing there may be more performant approaches. If it is looking up a value in another table for example it is often best to simply inline this logic into the query.

like image 171
Martin Smith Avatar answered Oct 20 '22 13:10

Martin Smith