Solved

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

Posted on 2006-11-10
2
751 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Folder Replication 4 59
SQL Insert Query Help, part2 6 57
DBF to ... Converter 5 38
Input Data Using a unique ID from another sheet in Excel 3 0
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

912 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

19 Experts available now in Live!

Get 1:1 Help Now