Microsoft KB Archive/264229

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

Article ID: 264229

Article Last Modified on 3/14/2006

-

APPLIES TO


 * Microsoft SQL Server 7.0 Standard Edition

-



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 &quot;More Information&quot; 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

Keywords: kbbug kbfix kbqfe KB264229

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.