?
Solved

SQL - Stored Procedure or Function inside a SELECT

Posted on 2007-08-01
5
Medium Priority
?
264 Views
Last Modified: 2008-01-09
Hi,

Is it possable to run a SQL Stored Procedure or a Function inside a SELECT statement?

If so, can you please give me an example? :)

Thank you!
0
Comment
Question by:Endelm
5 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 19610130
u can call functions inside a select statement

create function dbo.RetSum(@i int, @j int)
returns int
 as
begin
return (@i+@j)
end
go

SELECT dbo.RetSum(1,2)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19610358
>Is it possable to run a SQL Stored Procedure inside a SELECT statement?
no

>Is it possable to run a SQL Stored Function inside a SELECT statement?
yes, see comment of aneeshattingal
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 19611639
You can use a scalar function in place of any single value in a SELECT, so it can appear in the SELECT list, WHERE clause, etc..

You can use table-based functions in place of a table within a SELECT ** but the parameters to the table-based functions must be fully known at the start of the query, that is, you cannot use a result column of a query in a table-based function w/i a query**.  For example, this is OK:
DECLARE @date DATETIME
SET @date = GETDATE() - 30
SELECT ...
FROM dbo.aTableBasedFunction (@date)
since @date is known when the SELECT is started.  This is NOT allowed:
SELECT ...
FROM table1
CROSS JOIN dbo.aTableBasedFunction (table1.column1)
since "table1.column1" varies during the query, it cannot be resolved *prior* to running the SELECT, as required.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 19611652
For scalar functions, otoh, you can use table columns.  For example, this:

SELECT dbo.aScalarFunction(table1.column1) AS [Single-Value Function Result]
FROM table1
WHERE dbo.anotherScalarFunction(table1.column2) = 'ABC'

is OK.
0
 

Author Comment

by:Endelm
ID: 19616275
Thank you.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question