How to use Native Stored Procedures in Alpha Anywhere.
Stored Procedures are compiled SQL code stored in the database. Calling stored procedures as opposed to sending over query strings improves the performance of an application. Not only is there less network traffic since only short commands are sent instead of long query stings, but the execution of the actual code itself also improves. The reason is because a stored procedure is already compiled ahead of time. Stored procedures are also cached when they are run improving performance of subsequent calls.
Other than improving performance, stored procedures are also helpful because they provide another layer of abstraction for your application. For instance, you can change a query in a stored procedure and get different results without having to republish your app. Using stored procedures also makes your objects cleaner, and some data base management systems such as SQL Server provide tools that make it easy to back up your stored procedures.
Stored procedures can be utilized in Xbasic scripts or as the data source for reports or components.
Example: Calling SQL Stored Procedures
The following Xbasic script can be used to call a stored procedure. The script executes a stored procedure, called 'SalesByCustomerYear', and displays the total sales in a message box. The year and customer id are passed in as arguments to the stored procedure.
dim year as c = "1996" dim customer as c = "BOLID" dim cn as sql::Connection if (cn.open("::Name::myConnection")) then dim sql as c = "exec SalesByCustomerYear @year=:year, @cust_id=:customer" dim args as sql::Arguments args.set("year",year) args.set("customer",customer) if (cn.execute(sql,args) <> .f.) then dim totalSales as n = cn.ResultSet.data(1) ui_msg_box("Results","Total sales for " + customer + " in " + year + " : " + alltrim(str(totalSales,250,2,"$("))) else ' error ui_msg_box("Error","Error executing stored procedure. Error was: "+cn.callResult.text,UI_STOP_SYMBOL) end if else ' error ui_msg_box("Error","Error connecting to database. Error was: "+cn.callResult.text,UI_STOP_SYMBOL) end if
The stored procedure for this example is shown below. This stored procedure is based on the Northwind SQL Server database:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Description: Calculates the total for orders for a customer in a given year -- ============================================= CREATE PROCEDURE SalesByCustomerYear -- Add the parameters for the stored procedure here @cust_id Nchar(5), @year Nchar(4) AS SELECT SUM("Order Subtotals".subtotal) AS total FROM [Orders] INNER JOIN "Order Subtotals" ON [Orders].OrderID = "Order Subtotals".OrderID WHERE [Orders].CustomerID = @cust_id AND [Orders].ShippedDate BETWEEN '1/1/'[email protected] AND '12/31/'[email protected]
To learn more, check out the resources below.
- AlphaDAO - Stored Procedures and Output Arguments on the SQL Server
If you have a SQL server stored procedure that sets the value of an output argument, reading the value in the output argument, after the stored procedure has executed can be tricky. That's because you have to ensure that you first loop through all of the result sets that are returned by the stored procedure before reading the output arguments.
- Calling Stored Procedures in Server-side Code
Stored procedures can be called in server-side scripts in web and mobile applications to perform calculations or compute data to return to the client.