How to Use User-Defined Scalar Functions in Oracle and SQL Server
Description
User-defined scalar functions in Oracle and SQL Server can be called from Alpha Anywhere.
Discussion
You can either use the function in a SELECT statement, or execute the call in a PL/SQL script. The complete Xbasic scripts are shown below for both Oracle and SQL Server. In summary:
The correct syntax to select the result of a scalar function in Oracle is:
? c.Execute("select COUNT_EMP(:lower, :upper) as l_Count from dual")
?c.resultset.data("l_count")To retrieve output parameters from a stored procedure or function you can add an argument to the SQL::Arguments object for each output parameter as follows:
dim args as sql::arguments
args.add("l_count", 0, SQL::ArgumentUsage::OutputArgument)
? c.execute("begin :l_count := COUNT_EMP(:lower, :upper); end;", args)
? args[3].DataThe value SQL::ArgumentUsage::OutputArgument indicates that the parameter is output only. There is an enumerated value for each of (input, output and input/output).
The SQL is wrapped in a BEGIN/END pair. This seems to be required by OCI for the script to execute properly.
Calling a user defined scalar function using Oracle
Getting the result of the function using a SELECT statement
dim c as sql::connection
? c.open( ("::Name::Oracle") )?c.open( ("::Name::Oracle") )
dim args as sql::arguments
args.add("lower", 1)
args.add("upper", 100)
? c.execute("select COUNT_EMP(:lower, :upper) as l_Count from dual", args)
? c.resultset.data("l_count")Getting the result of the function using SQL::Arguments
dim c as sql::connection
? c.open( ("::Name::Oracle") )
dim args as sql::arguments
args.add("lower", 1)
args.add("upper", 100)
args.add("l_count", 0, SQL::ArgumentUsage::OutputArgument)
? c.execute("begin :l_count := COUNT_EMP(:lower, :upper); end;", args)
? args[3].DataCalling a user defined scalar function using SQL Server
Getting the result of the function using a SELECT statement
dim c as sql::connection
? c.open( ("::Name::SQLServer") )
dim args as sql::arguments
args.add("lower", 1)
args.add("upper", 100)
? c.execute("select dbo.COUNT_EMP(:lower, :upper) as l_Count", args)
? c.resultset.data("l_count")Getting the result of the function using SQL::Arguments.
dim c as sql::connection
? c.open( ("::Name::SQLServer") )
dim args as sql::arguments
args.add("lower", 1)
args.add("upper", 100)
args.add("l_count", 0, SQL::ArgumentUsage::OutputArgument)
? c.execute("select :l_count = dbo.COUNT_EMP(:lower, :upper)", args)
? args[3].Data