MS SQL Server与MS Access同公式计算结果不一致求助
Let's work through resolving this mismatch step by step, with practical, actionable steps:
1. First, Unpack Access's "Number" Type
Access’s Number field isn’t a single data type—it’s a label that maps to different underlying types based on the "Field Size" setting in your table design. The two most common causes of calculation mismatches are:
- Double: 64-bit floating-point (the default for Access Number if you don’t adjust settings; technically matches SQL Server’s
FLOAT(53), but calculation order/rounding timing can still differ) - Decimal: Fixed-precision numeric type (this is often the culprit if you see consistent rounding gaps, since SQL Server’s
FLOATis an approximate type whileDECIMALis exact)
First, open your Access table in Design View and check the Field Size property of every column used in your calculation. This will tell you exactly what type you’re working with.
2. Align SQL Server Data Types to Access
Once you know Access’s actual type, match it in SQL Server:
If Access uses Decimal (e.g., Field Size = Decimal, Precision = 18, Scale = 9):
Replace allFLOATcolumns in your view withDECIMAL(18,9)(adjust precision/scale to match your Access settings). For example:ALTER TABLE YourSampleTable ALTER COLUMN ColumnX DECIMAL(18,9);Rewrite your calculation using exact decimal arithmetic instead of floating-point operations—this will eliminate most rounding gaps.
If Access uses Double:
Ensure SQL Server usesFLOAT(53)(this is the default forFLOAT, but being explicit avoids confusion). The mismatch here usually comes from when each system rounds intermediate calculations.
3. Simulate Access’s Intermediate Rounding Behavior
Access often rounds intermediate calculation steps to the precision of the underlying type, while SQL Server may carry extra precision through the entire computation. To fix this, explicitly round intermediate steps in your SQL to match Access’s behavior.
For example, if your original calculation is:
SELECT id, (ColumnA / ColumnB) * ColumnC AS Emisn FROM YourSampleTable;
And Access rounds ColumnA / ColumnB to 15 significant digits (the precision of Double) before multiplying by ColumnC, rewrite it as:
SELECT id, ROUND(ColumnA / ColumnB, 15 - FLOOR(LOG10(ABS(ColumnA / ColumnB)))) * ColumnC AS Emisn FROM YourSampleTable;
This forces the division result to round to 15 significant digits, mirroring how Access handles Double-type calculations.
4. Validate with Your Sample Data
Using your sample results as a benchmark:
- For row 1, switching to Decimal or adding explicit rounding should align SQL’s 0.0000329819 with Access’s 0.0000439814
- For row 8, the small gap (6.2285552134 vs 6.2306588976) is likely due to floating-point precision differences that explicit rounding will resolve
内容的提问来源于stack exchange,提问作者Rick Savoy

