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.
Stored procedures can be called in web and mobile applications in server-side scripts. They can be used in sever-side events or Ajax Callbacks to compute information that is returned to the client. The two examples below demonstrate how to send data from the client to the server that is used to call a stored procedure to compute the total sales for a customer. The result is then sent back to the client where it is shown in a popup window or in a control on the component.
This example demonstrates calling a stored procedure in an Ajax Callback, which is called when the user clicks a button. The stored procedure computes the total sales for a customer for a given year. The UX Component contains two controls that specify the Year and Customer ID.
The Ajax Callback action is configured to call the getTotalSales Xbasic function (defined in the Xbasic functions section of the UX component), which is shown below:
The Xbasic function reads the year and customer values from the submitted data (e._currentRowDataNew.year and e._currentRowDataNew.customer) for use the SQL statement to call the stored procedure. After the stored procedure executes successfully, the result is returned to the client and displayed in a control on the UX Component called 'TOTALSALES'.
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/'+@year AND '12/31/'+@year
This example is based on the Grid Component example below. Watch the videos for the Grid Component example to learn more about calling stored procedures.