Chapter 5 Dynamo Tags Tutorial
These tutorials teach you how to use the most commonly used Dynamo tags.
Use the SQL Tag to embed SQL queries within HTML. You can then display the output in a variety of formats by using additional Dynamo tags.
This example shows how to embed a SQL query in HTML. This query requests the ID, first name, last name, and phone number of the customers in the customer table. The SQL block of information between the start SQL tag (<!--SQL) and the end SQL tag (-->) is processed by the Dynamo application server. The document is then passed on to the browser to be processed; the results display in HTML format.
<HTML>
<TITLE>SQL_tag.stm</TITLE>
<BODY>
<H1>Customer Information</H1>
<!--SQL
SELECT customer.id, customer.lname, customer.fname, customer.phone
FROM DBA.customer customer
-->
There are two ways to create a document with an embedded SQL query:
Using the Add Template wizard:
You can also create a document with an embedded SQL query by using the Add a Script wizard to create a script and then using the Sybase Central code editor to add the SQL tags and query by hand.
Using the Sybase Central code editor
The document you just created contains a simple embedded SQL query.
Next, we'll add a FORMATTING tag to display the results of the query.
Use the FORMATTING tag to display rows in the form of a table or a list. When you perform a query, you usually do not know how many rows will be returned.
To display the output as a simple table with no borders, headings or titles, enter:
<!--SQL
SELECT customer.id, customer.lname, customer.fname, customer.phone
FROM DBA.customer customer
-->
<!--formatting--><TR>
<!--/formatting-->
The template you created with the Add Template wizard automatically inserted FORMATTING tags. Your template should look something like this:
<HTML>
<TITLE>SQL_tag.stm</TITLE>
<BODY>
<H1>Customer Information</H1>
<!--SQL
SELECT customer.id, customer.lname, customer.fname, customer.phone
FROM DBA.customer customer
-->
<TABLE BORDER>
<TR>
<TH>id</TH>
<TH>lname</TH>
<TH>fname</TH>
<TH>phone</TH>
</TR>
<!--formatting--><TR>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
</TR><!--/formatting-->
</TABLE>
</BODY>
</HTML>
The <!--formatting--> tag indicates that the tags following it provide information on the formatting of the query. The <!--DATA--> tags represent data in a column. This example has four <!--DATA--> tags; one for each column being returned.
To assign column headings of the database table to the output, enter:
<TR>
<TH>id</TH>
<TH>lname</TH>
<TH>fname</TH>
<TH>phone</TH>
</TR>
You can use the Sybase Central code editor to edit formatting tags.
To alter a table heading with the Sybase Central code editor:
<TH>id</TH>
<TH>Customer ID</TH>
<TH>lname</TH>
<TH>Last Name</TH>
<TH>fname</TH>
<TH>First Name</TH>
You have now altered the table headings of your query. To view the template with these changes, right-click on the template within Sybase Central, and select Browse Output.
Yet another way to alter the formatting of your output is by using the LABEL tag. LABEL allows you to return the column name along with the column data. For example, you could set up a query to return output in this format:
id: 101
lname: Devlin
fname: Michael
phone: 2015558966
id: 102
lname: Reiser
fname: Beth
phone: 2125558725
id: 103
lname: Niedringhaus
fname: Erin
phone: 2155556513
To use the LABEL tag to format the output of a SQL query:
<HTML>
<TITLE>SQL_tag1.stm</TITLE>
<BODY>
<H1>Customer Information</H1>
<!--SQL
SELECT customer.id, customer.lname, customer.fname, customer.phone
FROM DBA.customer customer
-->
<!--formatting-->
<!--/formatting-->
</BODY>
</HTML>
<!--LABEL-->: <!--data--><BR>
Using HTML tags with Dynamo tags
You can use normal HTML tags with
the Dynamo tag to format the output. In this example we are using
the break (<BR>) tag to format the output in a
single column.
<HTML>
<TITLE>SQL_tag.stm</TITLE>
<BODY>
<H1>Customer Information</H1>
<!--SQL
SELECT customer.id, customer.lname, customer.fname, customer.phone
FROM DBA.customer customer
-->
<!--formatting-->
<!--LABEL-->: <!--data--><BR>
<!--LABEL-->: <!--data--><BR>
<!--LABEL-->: <!--data--><BR>
<!--LABEL-->: <!--data--><BR>
<BR>
<!--/formatting-->
</BODY>
</HTML>
Queries and manipulations against a database may sometimes generate errors. When errors occur, it is more useful to display a message to the user rather than an empty table of results. Dynamo includes several error-checking tags.
The next example first queries the product table from the Sample database, then adds error checking to ensure that no errors occur while the output is generated.
To use error checking tags in your template:
<HTML>
<TITLE>error_check.stm</TITLE>
<BODY>
<H1>Error Checking Example</H1>
<!--SQL
select name, quantity, unit_price, (quantity * unit_price) as total from product
-->
<TABLE BORDER>
<TR>
<TH>Product Name</TH>
<TH>Quantity</TH>
<TH>Unit_price</TH>
<TH>Total</TH>
</TR>
<!--formatting--><TR>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
</TR><!--/formatting-->
</TABLE>
</BODY>
</HTML>
<HTML>
<TITLE>error_check.stm</TITLE>
<BODY>
<H1>Error Checking example</H1>
<!--SQL
select name, quantity, unit_price, (quantity * unit_price) as total from product
-->
<!--SQL_ON_NO_ERROR-->
<TABLE BORDER>
<TR>
<TH>Product Name</TH>
<TH>Quantity</TH>
<TH>Unit_price</TH>
<TH>Total</TH>
</TR>
<!--formatting--><TR>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
</TR><!--/formatting-->
</TABLE>
<!--/SQL_ON_NO_ERROR-->
</BODY>
</HTML>
<HTML>
<TITLE>error_check.stm</TITLE>
<BODY>
<H1>Error Checking example</H1>
<!--SQL NO_SQL_ERROR
select name, quantity, unit_price, (quantity * unit_price) as total from product
-->
<!--SQL_ON_NO_ERROR-->
<TABLE BORDER>
<TR>
<TH>Product Name</TH>
<TH>Quantity</TH>
<TH>Unit_price</TH>
<TH>Total</TH>
</TR>
<!--formatting--><TR>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
</TR><!--/formatting-->
</TABLE>
<!--/SQL_ON_NO_ERROR-->
<!--SQL_ON_ERROR-->
<P>An error has occurred. Contact your system administrator.</P>
<!--/SQL_ON_ERROR-->
</BODY>
</HTML>
select name, quantity, unit_price, (quantity * unit_price) as total from prodduct
See the SQL_ERROR_CODE, SQL_ERROR_INFO, and SQL_STATE tags in "Dynamo Tags" in PowerDynamo Reference for information on how to display errors returned by the database.
Use the INCLUDE tag to incorporate generated output from a document into the current document. This can be useful when an identical SQL query is used by several different documents. Instead of maintaining and updating the query in each document, you can create one template containing the common query.
Let's say that you have an internal Web site that requires a the same list of customer contacts on several different Web pages. The easiest way to handle this is to create one template that queries the customer table for a list of contacts and then to include that template within all the Web pages that required that data.
To include the output of a document within another document:
<!--SQL
SELECT customer.company_name, customer.lname, customer.fname, customer.phone
FROM DBA.customer customer
-->
<TABLE BORDER>
<TR>
<TH>company_name</TH>
<TH>lname</TH>
<TH>fname</TH>
<TH>phone</TH>
</TR>
<!--formatting--><TR>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
<TD><!--data--></TD>
</TR><!--/formatting-->
</TABLE>
<HTML>
<H1>Customer List</H1>
<H2>Expectations</H2>
<P>Each sales rep is expected to contact the customers on their contact list a
minimum of once every two weeks. For a list of the customers that you are
responsible for please talk to you supervisor.</P>
</HTML>
<!--INCLUDE name="customer.stm"-->
<HTML>
<BODY>
<H1>Customer List</H1>
<!--INCLUDE name="customer.stm"-->
<H2>Expectations</H2>
<P>Each sales rep is expected to contact the customers on their contact list a
minimum of once every two weeks. For a list of the customers that you are
responsible for please talk to you supervisor.</P>
</BODY>
</HTML>
Use SQL_INSERT to create a Web page that updates data in a database.
For example, let's say you want to create an internal Web page for the sales group that allows the sales staff to add new customers to the contact table.
To create a Web page that updates the database:
<HTML>
<TITLE>newCustomer.stm</TITLE>
<BODY>
<H1>New Customer</H1>
<P>Enter the following information</P>
<FORM METHOD=POST ACTION="insertCust.ssc">
<OL>
<LI><INPUT TYPE="text" NAME="id" >ID of the customer<BR>
<LI><INPUT TYPE="text" NAME="last_name" >Last name of the customer<BR>
<LI><INPUT TYPE="text" NAME="first_name" >First name of the customer<BR>
<LI><INPUT TYPE="text" NAME="title" >Customer title<BR>
<LI><INPUT TYPE="text" NAME="street" >Street<BR>
<LI><INPUT TYPE="text" NAME="city" >City<BR>
<LI><INPUT TYPE="text" NAME="state" >State<BR>
<LI><INPUT TYPE="text" NAME="zip" >Zip Code<BR>
<LI><INPUT TYPE="text" NAME="phone" >Phone number<BR>
<LI><INPUT TYPE="text" NAME="fax" >Fax number<BR>
</OL>
<P><INPUT TYPE="submit"></p>
<P><INPUT TYPE="RESET" VALUE="Clear Form"></P>
</FORM>
</BODY>
</HTML>
<HTML>
<TITLE>insertCust.ssc</TITLE>
<BODY>
<H1>New Customer</H1>
<!--SQL_INSERT TABLE="contact" -->
<P>Click here <a HREF=NewCustomer.ssc>here </a> if you would like to add another customer</P>
<P>Click here <a HREF=Customer.stm>here </a> if you would like a listing of all customers</P>
</BODY>
</HTML>
Use the SCRIPT tag to embed Dynamo script language within documents. The format:
<!--SCRIPT
DynaScript
-->
lets Web editing tools know that they should ignore all text between the start tag (<!--SCRIPT) and end tags (-->) while also letting the Dynamo application server know that there is DynaScript present that requires processing.
For more information on the SCRIPT tag and DynaScript, see "The DynaScript Language" in PowerDynamo Reference.
Copyright © 1999 Sybase, Inc. All rights reserved. |