Basics of SQL Functions and Procedures
SQL is a well developed procedural language for writing scripts which can interact with your database. There are many links on-line which give deep and detailed explanations of how to write Procedural SQL. This short document is intended to give a basic overview and introduction of some key parts of the SQL that make up a Function or Procedure to help Orixa Developers extend their Apps.
Open the Database Management Utility to view details of a Procedure
To review and edit the Functions and Procedures in your App, go to the "System" menu, and select "View Database Management Utility", the screen showed in the image below should open.
Viewing the details of a Procedure
- Select the "Functions/Procedures" heading in the Database Treeview.
- Click on the name of a procedure (note that Procedures and Functions are held in separate lists, so click on the appropriate heading to see what you need). The SQL Definition of the procedure will then be shown at the far right-hard side of the screen.
- Once a Procedure is seleted a panel will list its parameters and allow you to Execute the procedure if you wish to.
Basic Elements of a Procedure
All Procedures and Functions contain SQL enclosed by "BEGIN" "END" sections. It is normal to DECLARE a number of variables, PREPARE and OPEN Cursors and FETCH data from the cursor.
In SQL the end of a line is set with a semi-colon. If a SQL-string is added it is included in single-quotes as shown in the examples, and will display red in the editor. Note that once a single-quote has been added the SQL-string can continue over several lines. The end of a line does not end the SQL-string. To add local variables or parameters to a SQL-string use the plus sign (+) to concatenant parts of a SQL-string together, as shown in the examples.
A common error when first writing SQL-strings within Procedures occurs when the SQL-string itself contains a single quote. In such cases the single-quote must be typed twice. This is called "escaping" the single quote, and allows single quotes to be included as part of the SQL-string.
DECLARE, PREPARE, FETCH |
|
Parameters, EXECUTE IMMEDIATE |
It is often useful to pass inputs from a user into a Procedure or Function. To do this add parameters to the Procedures definition
|
Configuration tables, COPY FILE | Procedures can be used to manage external data outside the database. To help with this there are a number of "Configuration" and "Information" Tables which can be queried with Statements and Cursors, to provide access to files on-disk. SQL Syntax includes a number of keywords which can be used to manipulate, copy, delete and alter files on-disk. Full details of this are beyond the scope of this document.
|
Using Parameters and Variables | Example showing the use of parameters and variables.
|
Basic Language Elements of Procedural SQL
- BEGIN …END; Used to demark the start and end of a function or procedure.
- IF [Boolean Statement] THEN [DoSomething] ELSE [DoAnotherThing] END IF; Used to create forking logic dependent on data and variables.
- WHILE [Boolean Statement] DO [DoSomething] END WHILE; Repetition logic, to do something. Remember with a While statement you must provide an exit, or the loop will run forever, so the [Boolean Statement] must be acted on within the While statement to reset it.
- EXECUTE IMMEDIATE [SQL Statement]; Allows one or more SQL statements to be run in a procedure.
- DECLARE Result [DataType]; [DoSomething] RETURN Result; This SQL syntax must be used within a Function to ensure that it returns the required value.
- FETCH FIRST/NEXT FROM [CursorName]('[FieldNames]') INTO [VariableName; Used to pull data out of a Cursor into a variable so it can be accessed by the Procedure.
- OPEN Crsr; Used to Open a data-set based on a previously prepared Statement. If this is the last line of a Procedure, then the procedure will return a data-set which will be viewed in your App.