TheInsider
Programmer
Hi,
I am using SQL Server 7 and I have run into a little dilemma. I have been working in Access for a few years and have come accustom to writing user-defined public functions in a VBA module when I need to do so. These functions can then be placed seamlessly into Access queries to do row-by-row calculations.
I am able to create the functions that I need in SQL Server using stored procedures, but apparently you can’t call a stored procedure from within a SELECT statement.
For example:
Then in Query Analyzer I would have something like this:
The above is a fictitious [and poor] example of what I am trying to do. I realize that A) the above example does not work, and B) there are much simpler ways to obtain that information in the latter case. My real situation is a little bit more complicated and the SELECT statement will be dynamically generated.
So far the only work-around that I can see is a complex series of sub-queries which wouldn’t be as fast or efficient as a stored procedure.
The problem is the fact that I need a row-by-row calculation in the SELECT statement, which is based on data in other tables.
Stored procedures seem perfect for creating user-defined functions. I am sure that other SQL Server developers do this all of the time, but how? PS. I have read Ion's FAQ in this forum, which simply states: no you can't.
Thanks
I am using SQL Server 7 and I have run into a little dilemma. I have been working in Access for a few years and have come accustom to writing user-defined public functions in a VBA module when I need to do so. These functions can then be placed seamlessly into Access queries to do row-by-row calculations.
I am able to create the functions that I need in SQL Server using stored procedures, but apparently you can’t call a stored procedure from within a SELECT statement.
For example:
Code:
-------------------------------------------------------------------
CREATE PROCEDURE spSumOrdersByID
@CustomerID int
AS
DECLARE @OrderSum money
SELECT @OrderSum = SUM(OrderPrice)
FROM Orders
WHERE CustomerID = @CustomerID
RETURN @OrderSum
--------------------------------------------------------------------
Code:
SELECT CustomerID, CustomerName, EXEC spSumOrdersByID CustomerID AS OrderTotal FROM Customers
The above is a fictitious [and poor] example of what I am trying to do. I realize that A) the above example does not work, and B) there are much simpler ways to obtain that information in the latter case. My real situation is a little bit more complicated and the SELECT statement will be dynamically generated.
So far the only work-around that I can see is a complex series of sub-queries which wouldn’t be as fast or efficient as a stored procedure.
The problem is the fact that I need a row-by-row calculation in the SELECT statement, which is based on data in other tables.
Stored procedures seem perfect for creating user-defined functions. I am sure that other SQL Server developers do this all of the time, but how? PS. I have read Ion's FAQ in this forum, which simply states: no you can't.
Thanks