You could run the new CREATE statement using the tools in the Data
perspective, but sometimes you may need to run custom SQL code in your EGL
application. Obviously, any time you insert customized SQL code into an EGL
application, you should test it carefully to make sure it does what you expect
it to do.
The EGL
execute statement lets you run
SQL code without dealing with SQLRecord variables. You can use
execute to
run a variety of SQL statements, including CREATE, ALTER, and DROP TABLE,
INSERT, DELETE, and UPDATE. For example, you can remove a table from the database
like this:
execute #sql{
DROP TABLE OLD_TABLE;
}
The execute statement doesn't support
some SQL statements, most notably SELECT and OPEN. For a list of which SQL
statements work or do not work with execute, see execute
considerations for SQL.
You can also use
execute to
call a stored procedure:
execute #sql{
call aStoredProcedure( :parameterVar)
};
Derby does not support stored procedures in the same way as
most database management systems, so this tutorial will not cover stored procedures.
Instead, in this section, you will use
execute to
create a table in the database and populate it with data without using any
SQLRecord parts.
- Return to the EGL perspective.
- Create a new Library part in a file named CustomerTableOperations.egl in
a package named libraries.
- Insert new functions into the library named execCreateTable, execInsertIntoTable,
and execDropTable, with each function receiving a StatusRec
record variable as a parameter:
function execCreateTable(status StatusRec inOut)
end
function execInsertIntoTable(status StatusRec inOut)
end
function execDropTable(status StatusRec inOut)
end
The StatusRec record variable records information about the
success or failure of database operations.
- If you did not use content assist to add the parameter, add the
following import statement to the top of the file,
just below the package statement:
import eglderbyr7.StatusRec;
- Using an execute statement in the execCreateTable
function, run the CREATE TABLE statement you created in the previous section:
function execCreateTable(status StatusRec inOut)
try
execute
#sql{
CREATE TABLE EGL.CUSTOMERTEMP (
CUSTOMER_ID INTEGER NOT NULL,
FIRST_NAME VARCHAR(30),
LAST_NAME VARCHAR(30),
PASSWORD CHAR(8),
PHONE VARCHAR(14),
EMAIL_ADDRESS VARCHAR(50),
STREET VARCHAR(30),
APARTMENT VARCHAR(10),
CITY VARCHAR(30),
STATE CHAR(2),
POSTALCODE VARCHAR(10),
DIRECTIONS VARCHAR(255)
)
};
onException (exception SQLException)
ConditionHandlingLib.HandleException(status, exception);
end
end
This function creates the table and then handles any errors
using the HandleSuccess function that is created along with the data parts
and logic parts from the database.
- If you did not use content assist to add this code, add the following import statement
to the top of the file:
import eglderbyr7.ConditionHandlingLib;
- In the execDropTable function, add an execute statement
that removes the table from the database:
function execDropTable(status StatusRec inOut)
try
execute #sql{
DROP TABLE EGL.CUSTOMERTEMP
} ;
onException (exception SQLException)
ConditionHandlingLib.HandleException(status, exception);
end
end
- In the execInsertIntoTable function, add an execute statement
that moves records from the CUSTOMER table into the CUSTOMERTEMP table:
function execInsertIntoTable(status StatusRec inOut)
try
execute #sql{
INSERT INTO EGL.CUSTOMERTEMP
SELECT * FROM EGL.CUSTOMER
} ;
onException (exception SQLException)
ConditionHandlingLib.HandleException(status, exception);
end
end
This code merely copies each record from CUSTOMER into CUSTOMERTEMP.
If you want, you can add a WHERE clause to select only certain records to
copy.
- Save and generate the library.
- Create a new Program part in a file named duplicateTable.egl in
the programs package.
- In the new program, call the functions that create the new table
and insert records into it:
package programs;
import eglderbyr7.StatusRec;
import libraries.CustomerTableOperations;
program duplicateTable type BasicProgram {}
status StatusRec;
function main()
CustomerTableOperations.execCreateTable(status);
CustomerTableOperations.execInsertIntoTable(status);
end
end
- Generate and run the program.
- Return to the Data perspective and reconnect to the database in
the Database Explorer view.
- As you did in the previous section, preview the data in the new
table to see the new table and its data:
- Expand .
- Right-click the CUSTOMERTEMP table and then click .
Now you can see the data in the new table:
Now
you can create other statements to work with the new table, or you can call
the execDropTable function to remove it from the database. For example, you
might execute the ALTER TABLE and CREATE INDEX statements that were created
along with the CREATE TABLE statement, but if you do, you must rename the
constraint and index, because Derby does not allow duplicate names for primary
key constraints. Remember to disconnect from the database in the Database
Explorer view before running any EGL code that accesses the database.
Here
is the complete code of the two files used in this lesson. If you see any
errors marked by red X symbols in either file, make sure your code matches
this code:Completed CustomerTableOperations.egl and duplicateTable.egl files after lesson 6.