Solved

T-SQL/ADO - Can you call a UDF directly from ADO?

Posted on 2006-11-10
2
757 Views
Last Modified: 2009-12-16
Just a quick question...is there a way to call a UDF from ADO without using a wrapper stored procedure?

For example, here's a mock-up function:

CREATE FUNCTION fn_UserExists (@intUserID INT)
RETURNS INT
BEGIN
DECLARE @RetCode INT
SET @RetCode = 0
SELECT @RetCode = CASE WHEN @intUserID IN (SELECT intUserID from tblUser) THEN 1 ELSE 0 END
RETURN @RetCode
END

I'd like to call it from an ASP page using the ADO Command object.  The only way so far that I've gotten to work is to create a wrapper procedure like this:

CREATE PROCEDURE sp_UserExists (@intUserID INT)
AS
BEGIN
DECLARE @RetCode INT
SET @RetCode = 0
SELECT @RetCode = dbo.fn_UserExists(@intUserID)
RETURN @RetCode
END

Then I call it like this:

Function UserExists (conn, intUserID)
      Dim cmd
      Set cmd = server.createobject("ADODB.Command")

      cmd.CommandText = "sp_UserExists"
      cmd.ActiveConnection = conn
      cmd.CommandType = adCmdStoredProc
      cmd.Parameters.Append cmd.CreateParameter("return", adInteger, adParamReturnValue, 4, 0)
      cmd.Parameters.Append cmd.CreateParameter("@intUserID", adInteger, adParamInput, 4, intUserID)
      cmd.Execute

      UserExists = cmd.parameters("return").value
      Set cmd = Nothing
End Function



I need the function to call in other situations within SQL but it seems redundant to have a proc out there just to call the function.  Does anyone know of a better way?

Thanks in advance!
0
Comment
Question by:ShantiSmurf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 50 total points
ID: 17917666
From ADO, you execute this string SQL Statement:
"SELECT dbo.fn_UserExists (" & myint & ")"

That might work.
0
 

Author Comment

by:ShantiSmurf
ID: 17917788
Perfect!  It's so simple, I can't believe I didn't see it earlier hehe  That's just what I was looking for.  I'm calling it like this:

UserExists = conn.Execute("SELECT dbo.fn_UserExists (" & intUserID & ")").GetString()

Just right!  Thanks!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

738 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