Skip to main content

Using a Dynamic SQL Query

In fact, we're going to use a different type of query. Rather than a query, such as TopFilms, that is predefined in the class definition...

—TopPicks.csp—
TopPicks.csp
 <csp:query name=FilmList classname="Cinema.Film" queryname="TopFilms">

... we're going to use a dynamic query that is defined via SQL. We can do this with a <script> tag that specifies language="sql" and that encloses the actual SQL SELECT request.

The query shown here includes the same columns as TopFilms, but it displays all films and it sorts them by title, rather than by number of tickets sold.

—SearchResults.csp—
SearchResults.csp
<script language="sql" name=FilmList>
    SELECT ID, Description, Length, Rating, Title, Category->CategoryName
    FROM Cinema.Film
    WHERE PlayingNow = 1
    ORDER BY Title
</script>
FeedbackOpens in a new tab