Creating a Delete Function Call

When you create a delete function call in XQuery Mapper, you must

l Use the XQuery Mapper short-cut menu
l Map the function block's output port to the Set Target Document pane.

How to Create a Delete Function Call

To create a delete function call:
1. Right-click the Mapper canvas.
2. Select Function Block > DataDirect XQuery > sql-delete from the short-cut menu.
The delete function block appears on the Mapper canvas.
3. Drag the table you want to update from the File Explorer and drop it on the Add Source Document pane.

Example

In this example, we delete one of the products from the Products table, Queso Cabrales, as it as been discontinued by the supplier. As shown in the following illustration, Queso Cabrales corresponds to ProductID 11 in the Products table:

In a new XQuery we save as DELETE.xquery, we first add the Products table to our XQuery by dragging it from the File Explorer and dropping it on the Add Source Document pane. We start building the XQuery by creating a FLWOR block; we'll use the FLWOR expression to iterate over the Products table to locate the record we want to delete. When the FLWOR block is added, we map the Products repeating element to the FLWOR block's For port.

Next, we add a simple condition expression that locates the Product table record by matching the ProductID to the value of the record we want to delete (the ProductID for Queso Cabrales is 11). We can then map the output of this condition expression to the Where port on the FLWOR block.

Finally, we add the SQL delete function by right-clicking the Mapper canvas and selecting Function Block > DataDirect XQuery > sql-delete from the short-cut menu. (SQL delete functions cannot be created using drag-and drop.) We map the FLWOR block's output port to the flow port on the top of the SQL delete function block, and map the Products repeating element to the SQL delete function block's input port. At this point, our XQuery looks something like this:

Notice that the text pane of the XQuery Mapper is blank. This is because the mapping has not been committed to Stylus Studio. To commit a mapping to Stylus Studio, you simply map the function block's output port to the Set Target Document pane. This mapping, which is completed automatically for you by Stylus Studio when you create SQL insert and update functions using drag and drop, is needed to commit the XQuery mapping to Stylus Studio; as with SQL insert and update functions, no output results from a SQL delete function. Any changes specified in the XQuery occur directly on the database.

When we commit the XQuery mapping to Stylus Studio, our XQuery code appears in the text pane, as shown here:

After we preview the XQuery, we can see that the record for Queso Cabrales has been deleted from the Products table:

 
Free Stylus Studio XML Training: