| home -> documentation -> environment guide -> Modifying information: The QRYEXEC tag |
Until this moment, we have only been retrieving information from the database. But now is time to make some modifications to the data according to our requirements. This is done with the QRYEXEC tag.
The QRYEXEC tag
The QRYEXEC tag is used to insert, update or delete records. It could be used also for executing stored procedures that modify any database information.
You should use the QRYEXEC tag when the SQL sentence does not return a result set. Otherwise, if you are interested in using some information returned after the execution of the query, you must use the DATASET tag instead.
QRYEXEC Tag modes
There are two possible modes using the QRYEXEC tag:
Mode A: Immediate execution
|
<#QRYEXEC ALIAS="DEMO" QUERYID="12"/>
|
This query is executed immediately after being interpreted.
Mode B: Response execution
|
<#QRYEXEC ALIAS=DEMO" QUERYID="12" DOC="page2.dbsp"/>
|
In this case, the QRYEXEC tag is substituted by an URL. When this URL became requested, the assigned query will be executed and the defined document will be sent as the response. You could use this mode, in an HTML anchor or in the action property of a form. Check both modes in the examples section.
Related topics
Examples
Example 1. A simple insert and delete.
This example shows you how to insert and delete records from a table.
Source code
File: examples/Ex6_5_1.dbsp
<html> <head> <title>QRYEXEC Example</title> <head> <script language="javascript"> function Validate () { if ( (document.frmAdd.CONTACTNAME.value=='') || (document.frmAdd.EMAIL.value=='')) { alert ('Both fields must contain information!'); return false; } else { return true } } function ClearTable () { <!-- This function will delete the contacts table !--> if (confirm ('Do you want to delete all the records on the table?')) { document.location.replace('<#QRYEXEC ALIAS=OPEX QUERYID=15 DOC=Ex6_5_1.dbsp/>'); } } </script> <!-- Prevent that the page remains stored in the browsers cache --> <#HEADER NAME="Cache-Control" VALUE="no-cache,no-store"/> </head> <body> <h1>QRYEXEC Example</h1> <hr> <p>This example shows a simple insertion into a table named CONTACTS. We include a very simple validation to avoid null insertions in any of the fields.</p> <table cellpadding="1" border=1 cellspacing="0"> <tr> <th width="250">Name</th> <th width="150">e-mail</th> </tr> <!-- Contacts table !--> <#DATASET ALIAS="OPEX" QUERYID="13"> <tr> <td><#USERNAME/></td> <td><#EMAIL/></td> </tr> <#NODATA> <!-- If the table is empty!--> <tr> <td colspan="2" align="Center">There are no contacts registered.</td> </tr> <#/DATASET> </table> <br> <button onclick="javascript:ClearTable()">Clear table</button> <br><br> <hr> <h3>Add a new contact</h3> <!-- Notice that the action property of the form contains a QRYEXEC Tag with a DOC property set to this file. This means, that the query 14 will be executed and then this page will be retreived again. --> <form name="frmAdd" method="post" onsubmit="return Validate()" action="<#QRYEXEC ALIAS=OPEX QUERYID=14 DOC=Ex6_5_1.dbsp/>"> <!-- We use the MAXLEN property because both fields on the contacts table have a physical length of 128 characters. If we don't validate this we could get different errors depending on the RDBMS !--> <B>Name * </B> <input type="text" name="CONTACTNAME" maxlength="128" size="30"><br> <B>e-mail * </B> <input type="text" name="EMAIL" maxlength="128" size="15"> <br><br> <input type="submit"> </form> </body> </html>
|
SQL Sentence: ID=13
SQL Sentence: ID=14
|
INSERT INTO CONTACTS (USERNAME, EMAIL)
VALUES
('<#CONTACTNAME>','<#EMAIL>')
|
SQL Sentence: ID=15
Test example
Example 2. Inserting multiple records.
This example shows you how to insert multiple records into a table.
Source code
File: examples/Ex6_5_2.dbsp
<html> <head> <title>Multiple insertion example</title> <head> <script language="javascript"> function ClearTable () { <!-- This function will delete the contacts table !--> if (confirm ('Do you want to delete all the records on the table?')) { document.location.replace('<#QRYEXEC ALIAS=OPEX QUERYID=15 DOC=Ex6_5_2.dbsp/>'); } } </script> <!-- Prevent that the page remains stored in the browsers cache --> <#HEADER NAME="Cache-Control" VALUE="no-cache,no-store"/> </head> <body> <h1>Multiple insertion example</h1> <hr> <p>This example shows how to insert multiple records in a single request to the server.</p> <table cellpadding="1" border=1 cellspacing="0"> <tr> <th width="250">Name</th> <th width="150">e-mail</th> </tr> <!-- Contacts table !--> <#DATASET ALIAS="OPEX" QUERYID="13"> <tr> <td><#USERNAME/></td> <td><#EMAIL/></td> </tr> <#NODATA> <!-- If the table is empty!--> <tr> <td colspan="2" align="Center">There are no contacts registered.</td> </tr> <#/DATASET> </table> <br> <button onclick="javascript:ClearTable()">Clear table</button> <br><br> <hr> <h3>Add multiple contacts</h3> <p>When you use a multiple execution statement, the first field in the MULTIVALUEPARAMS list must not be null (in this case, the NAME field). Otherwise, the entire row is ignored and the statement does not execute for this particular row.</p> <!-- Notice that the action property of the form contains a QRYEXEC Tag with a DOC property set to this file. This means, that the query 14 will be executed for each row and then this page will be retreived again. --> <form name="frmAdd" method="post" action="<#QRYEXEC ALIAS=OPEX QUERYID=14 MULTIVALUEPARAMS="CONTACTNAME,EMAIL" MULTIVALUERANGE="1..5" DOC=Ex6_5_2.dbsp/>"> <!-- We use the MAXLEN property because both fields on the contacts table have a physical length of 128 characters. If we don't validate this we could get different errors depending on the RDBMS !--> <#FOR NAME="COUNTER" STARTIN="1" ENDIN="5"> <#COUNTER/>. <B>Name * </B> <input type="text" name="CONTACTNAME<#COUNTER/>" maxlength="128" size="30"> <B>e-mail * </B> <input type="text" name="EMAIL<#COUNTER/>" maxlength="128" size="25"> <br> <#/FOR> <br><br> <input type="submit"> </form> </body> </html>
|
SQL Sentence: ID=13
SQL Sentence: ID=14
|
INSERT INTO CONTACTS (USERNAME, EMAIL)
VALUES
('<#CONTACTNAME>','<#EMAIL>')
|
SQL Sentence: ID=15
Test example
Previous | Next
|