Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

747 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