Sybase : Iterating over table without using cursor

Cursor is not the only way to iterate over data in a table in sybase. It is possible to iterate over a table without using cursor. This article shows an alternative to cursor in sybase.

If a table has a clustered index defined on a column (or set of columns) then the rows in that table are organized in a sorted order by the indexed columns. This property of the clustered index can be used to iterate over the rows of a table.

Let us consider a table as below.

order_id product_name quantity status
1 Soap 4 Unknown
2 Toothpaste 2 Unknown
3 Shampoo 3 Unknown

The table has a clustered index on order_id column. The status column needs to be updated one row at a time. This may be required if the status is received as an output of a stored procedure.

Pick up a control variable and set it to minimum possible value. In this case a variable to hold order id, set to zero. Set rowcount to 1. This is the real trick. It causes only one row to be returned for a select query.

declare @prev_order_id int
select  @prev_order_id = 0
declare @nrows int
select @nrows = 1

while @nrows > 0
begin
    set rowcount 1

Now select data from table and perform operation using the data. Also select number of rows affected from the select operation. This must be stored in another variable because value of @@rowcount may subsequently change.

    select @product = product_name, @prev_order_id = order_id from ORDER where order_id > @prev_order_id
    select @nrows = @@rowcount
    -- perform operation using @product

End the loop and set row count to zero. Setting to zero returns all the rows affected.

end

set rowcount 0

Complete listing of the code below.

declare @prev_order_id int
select  @prev_order_id = 0
declare @nrows int
select @nrows = 1

while @nrows > 0
begin
    set rowcount 1
    select @product = product_name, @prev_order_id = order_id from ORDER where order_id > @prev_order_id
    select @nrows = @@rowcount
    -- perform operation using @product
end

set rowcount 0

 

Photo credit: katerha / Foter.com / CC BY

Floating help box with jQuery UI tooltip

jQuery UI has a nice tooltip widget. This widget is used to display a theamable tooltip for elements on a web page. That is the primary and intended use of the tooltip widget. However this widget can be innovatively used to display a startup help box.

Startup helpbox is used to display a quick instruction to the user. It is intended to get user started with the website or application. This help box disappears as soon as user starts using the application by typing, clicking etc. Lets consider a simple application that gets a string from user and shows length of the string. A startup help box could say, “Enter text below to see the number of characters entered.” The help box would disappear as soon as user starts typing in the text box. There would be a help button that when hovered would show the help box again. Lets see how to create such a help box.

To use jQuery UI tooltip, include jQuery UI library in your webpage. Normally, to create a tooltip for an element, title attribute is set on the this element and the tooltip widget is initialised on this element. The tooltip appears when mouse is hovered over this element.

However the startup help box would apppear as soon as the page loads whether or not the intended element is hovered or not. Also the tooltip would disappear when user starts to interact with the page. To achieve this, set the title attribute on a completely different element. For our string length web application, set the title attribute on the help button. Set the message that should be displayed in the help box as value of the title attribute. Also create a text input box where user would enter string.

<input id="stringInput"></input>
<span id="helpbutton" title="Enter text to find out number of characters entered">?</span>

Initialise the tooltip and set position on the intended element.  This would associate the tooltip with the help button but we want to see the tooltip next to the input text box. Hence set the position of the tooltip to the right hand side of the input text box. Unlike the simple tooltip, the help box should open on page load. This is achieved by calling open on the tooltip.

$("#helpbutton").tooltip({position: {of: "#stringInput", at: "right"}});
$("#helpbutton").tooltip("open");

The help box should close when user starts to type in the input box. Call close on the tooltip on keyup event of the input box. The help box is also closed when user clicks on the input box.

$("#stringInput").keyup(function()
{
    $("#helpbutton").tooltip("close");
});
$("#stringInput").click(function()
{
    $("#helpbutton").tooltip("close");
});

That is it! A simple trick to place a tooltip on a different element and opening and closing the tooltip on page load and click event creates a nice startup help box.

 

Photo credit: Mykl Roventine / Foter.com / CC BY-NC-SA