Solved

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

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL BACKUP - 2008 R2 8 70
multiple application databases same MSSQL instance 5 53
SYbase 4 29
SQL SELECT query help 7 34
APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

776 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