September 9, 2013 at 11:40 pm
Hi Every one,
We all Knows that we call function through select and Sp through Exec.My Question is why can't we call the Sp
through Select .is there any hidden Reason behind that?
September 10, 2013 at 12:49 am
A function has a fixed output in the sense that the metadata of the columns returned is known upfront. Functions are thus meant to return data.
Stored procedures however can do anything. They are meant to do something, not necessarily return data. If they return data at all, the metadata is not known upfront.
If you want something to return data and use it in a select, then write a fuction.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
September 10, 2013 at 3:36 am
Yep. Exactly. Nothing hidden there. They are different constructs doing different things.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
September 10, 2013 at 4:32 am
Are you asking this because you are curious, or because you want to use the output of a stored procedure as an input for a query? There are ways...
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 10, 2013 at 10:18 pm
thanks grant frinchy. now i am reading the book sql-server-execution plans by you.extraordinary book for execution palns.
September 12, 2013 at 5:20 pm
Some more points:
- Functions have some limitations (can't execute stored procedures or use temporary tables for example). Occasionally, this can bite because TTs allow for more flexibility with indexes/statics etc.
- Functions can be called from anywhere whereas procs can't be executed within a Function or Select/View (the "INSERT #TempTable EXEC proc" pattern excepted). This makes functions a more flexible candidate for layered implementation of business-rules.
September 13, 2013 at 9:37 am
John Chapman (9/12/2013)
Some more points:- Functions have some limitations (can't execute stored procedures or use temporary tables for example). Occasionally, this can bite because TTs allow for more flexibility with indexes/statics etc.
- Functions can be called from anywhere whereas procs can't be executed within a Function or Select/View (the "INSERT #TempTable EXEC proc" pattern excepted). This makes functions a more flexible candidate for layered implementation of business-rules.
However you can use a common table expression in a function.
Kurt
Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY
http://www.linkedin.com/in/kurtwzimmerman
September 13, 2013 at 1:41 pm
Ananth@Sql (9/9/2013)
Hi Every one,We all Knows that we call function through select and Sp through Exec.My Question is why can't we call the Sp
through Select .is there any hidden Reason behind that?
Ah, but you CAN call a stored procedure though a SELECT using OPENROWSET. If you need to pass parameters to the sproc, it becomes a bit of a PITA because you have to use dynamic SQL, but it can be done.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 14, 2013 at 3:22 am
Jeff Moden (9/13/2013)
Ah, but you CAN call a stored procedure though a SELECT using OPENROWSET. If you need to pass parameters to the sproc, it becomes a bit of a PITA because you have to use dynamic SQL, but it can be done.
But it is an awfully bad thing to do. And it will not work with all stored procedures.
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply