Solved

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cassandra Select Query 1 87
VBA MS Word "Table of Contents" extractor` 4 57
constraint check 2 48
best datatype for oracle table email creation 8 58
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

820 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