FIX: SELECT Statement Assigns Incorrect Values to NUMERIC or DECIMAL Variables When Used Multiple Times (264229)



The information in this article applies to:

  • Microsoft SQL Server 7.0

This article was previously published under Q264229
BUG #: 57925 (SQLBUG_70)

SYMPTOMS

If a SELECT statement uses the same variable multiple times in an expression to assign it to different variables, you might get incorrect results.

This problem only occurs when you use numeric and decimal datatypes.

Please see the "More Information" section of this article for a detailed description of the problem.

CAUSE

Microsoft SQL Server calculates a value for the variable once, and then reuses the same memory address for further calculations before it outputs the value.

WORKAROUND

To work around this behavior, assign only one value to a single variable for each SELECT statement.

STATUS

Microsoft has confirmed this to be a problem in SQL Server 7.0. This problem has been corrected in U.S. Service Pack 3 for Microsoft SQL Server 7.0. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

274799 INF: How to Obtain Service Pack 3 for Microsoft SQL Server 7.0 and Microsoft Data Engine (MSDE) 1.0

For more information, contact your primary support provider.

MORE INFORMATION

This can be further illustrated by the following example:
use pubs
go

drop table t1
go
create table t1(na int)
go
insert into t1 values(2)
go
 
declare @a numeric(9,0), @b numeric(9,0)
select @a = (2 * t1.na), @b = @a - 1 from t1 where na = 2
select @a as [a], @b as [b]
select @a = (2 * t1.na), @b = @a - 2 from t1 where na = 2
select @a as [a], @b as [b]
select @a = (2 * t1.na), @b = @a - 3 from t1 where na = 2
select @a as [a], @b as [b]
go
				
Returns:
a           b           
----------- ----------- 
3           3			--> should be 4, 3

a           b           
----------- ----------- 
2           2			--> should be 4, 2

a           b           
----------- ----------- 
1           1			--> should be 4, 1
				

Modification Type:MajorLast Reviewed:3/14/2006
Keywords:kbBug kbfix kbQFE KB264229