SQL Scalar Function to call stored procedure with output parameter

Posted on 2011-10-12
Last Modified: 2012-06-27
I have a SQL Server 2005 backend with some existing stored procedures that work well.  One I'm dealing with has a return value and is defined like this:

ALTER PROCEDURE [dbo].[uspUserHasPermission] @p_uID varchar(8), @p_permit varchar(64), @p_authPermit int OUTPUT AS

I need this to remain as it is because it is used throughout the application in this manner, but I now need an additional use of it to perform as a scalar function to be used in a query so that I don't have to have two versions of the same functional code to maintain.  I just want to create a scalar function that calls [uspUserHasPermission] and returns the output from that usp.  So I created the following:

CREATE FUNCTION fnUserHasPermission 
	@p_uID varchar(8), 
	@p_permit varchar(64)	
RETURNS int	--, @p_authPermit int OUTPUT
	DECLARE @p_authPermit as int

	EXECUTE [uspUserHasPermission] @p_uID, @p_permit, @p_authPermit OUTPUT

	RETURN @p_authPermit


Open in new window

...and when I try to execute it, I get the response:

Msg 557, Level 16, State 2, Line 1
Only functions and extended stored procedures can be executed from within a function.

...which I interpret to mean that a scalar function cannot call a stored procedure with a return value the way I have it.

Is this correct?  Are there any suggestions on how I can easily call a stored proc that returns a scalar value without having to repeat the complete usp code?
Question by:kbirecki
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    >...which I interpret to mean that a scalar function cannot call a stored procedure with a return value the way I have it.

    and I know no alternative so far ;(
    LVL 11

    Author Comment

    I just thought of switching them around.  I could move the meat of the usp to the scalar and call the scalar from the usp.  Maybe that is the best option, huh?
    LVL 142

    Accepted Solution

    actually, if both the procedure and the function are yours to be modified, yes that works

    I thought the procedure called was a system proc or some proc that shall not be modified.
    LVL 11

    Author Comment

    Excellent.  Thank you!
    LVL 11

    Author Closing Comment

    Thanks for the follow-up.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now