cancel
Showing results for 
Search instead for 
Did you mean: 

Significant difference between SQL/Anywhere and MS SQL/Server - CURSOR variable evaluation

0 Kudos
5,318

This may be a documentation or standards concern, but the function below demonstrates very different results between SQL/Anywhere (12.0.0) and MS SQL/Server (2008 R2). On SQL/Anywhere, this function always returns 2, but on SQL/Server, it returns the value of the parameter passed in. In other words, the return value hinges on when @parm1 is evaluated: for SQL/Anywhere, it's at the OPEN, for SQL/Server, it's at the DECLARE. Microsoft documents this as "Variables may be used as part of the select_statement that declares a cursor. Cursor variable values do not change after a cursor is declared. In SQL Server version 6.5 and earlier, variable values are refreshed every time a cursor is reopened." I didn't see this mentioned in the SQL/Anywhere documentation, though I didn't look too hard - is it an SQL/92 vs SQL/2008 standards discrepancy?

ALTER FUNCTION fncursorTest( @parm1 INTEGER )
RETURNS INTEGER
AS
BEGIN
    DECLARE @retval INTEGER;
    DECLARE cursor1 CURSOR FOR
        SELECT @parm1;
    SET @parm1 = 2;
    OPEN cursor1;
    FETCH cursor1 INTO @retval;
    CLOSE cursor1;
    DEALLOCATE cursor1;
    RETURN @retval;
END

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member

In SQL Anywhere, a variable used in a query is evaluated no earlier than at the cursor OPEN time, and possibly as late as FETCH time.

0 Kudos

Elmi, thanks for your comment,

  1. Do you know if this is described in the SQL Anywhere documentation somewhere (it's not mentioned in the "SQL dialects and compatibility" / "SQL Anywhere features that differ from other SQL implementations" section. Is this compatible with Adaptive Server Enterprise?)
  2. Does this conform to SQL/2008 or is the standard too vague on the issue?
Former Member
0 Kudos
  1. As far as I can tell, there is no description of the behaviour in the SQL Anywhere documentation.
  2. SQL/2011 standard, Foundation, Section 15.1, General Rules 5)a )i) implies that all variables used in a cursor are evaluated at OPEN time.