SQL Server 中的“金钱”[...] 数据类型如何存储在内存中?
如果您想查看如何存储 MONEY(8 字节)值,则可以执行以下脚本步骤/步骤:
CREATE DATABASE TestMoneyDT;
GO
USE TestMoneyDT;
GO
CREATE TABLE dbo.MyMoney -- :-)
(
Col1 CHAR(5) NOT NULL,
Col2 MONEY NOT NULL,
Col3 CHAR(5) NOT NULL
);
GO
INSERT dbo.MyMoney (Col1, Col2, Col3)
VALUES ('AAAAA',12345678.0009,'BBBBB');
GO
-- Install http://www.sqlskills.com/blogs/paul/inside-the-storage-engine-sp_allocationmetadata-putting-undocumented-system-catalog-views-to-work/
EXEC sp_AllocationMetadata 'dbo.MyMoney'
GO
/*
Stored procedure output:
Object Name Index ID Alloc Unit ID Alloc Unit Type First Page Root Page First IAM Page
----------- -------- ----------------- --------------- ---------- --------- --------------
MyMoney 0 72057594039697408 IN_ROW_DATA (1:147) (0:0) (1:150)
*/
SELECT DB_ID() AS DBID
GO
/*
DBID
----
13
*/
-- Reading data from page (1:147) (file id 1, page number 147)
DBCC TRACEON(3604); -- http://technet.microsoft.com/en-us/library/ms187329.aspx
DBCC PAGE(13, 1, 147, 3); -- http://blogs.msdn.com/b/sqlserverstorageengine/archive/2006/06/10/625659.aspx
DBCC TRACEOFF(3604); -- http://technet.microsoft.com/en-us/library/ms174401.aspx
GO
-- See [Memory dump @0x0000000014AEA060] of DBCC PAGE output
/*
Memory Dump @0x000000000E76A060
0000000000000000: 10001600 41414141 41e9f698 be1c0000 †....AAAAAéö.¾...
0000000000000010: 00424242 42420300 00†††††††††††††††††.BBBBB...
41414141 41 = AAAAA <- Col1 CHAR(5)
e9f698 be1c0000 <- Col2 MONEY take this string and run following script (look at SumOverAll values)
424242 4242 = BBBBB <- Col3 CHAR(5)
*/
GO
DECLARE @HexString VARBINARY(8) = 0xE9F698BE1C; -- One MONEY value consumes 8 bytes
WITH N10
AS
(
SELECT *
FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) x(Num)
)
SELECT src.*,
SUM(src.IntValueMultipliedByte) OVER() AS SumOverAll
FROM
(
SELECT n.Num,
SUBSTRING(@HexString, n.Num, 2) AS HexValue,
CONVERT(INT, SUBSTRING(@HexString, n.Num, 1)) AS IntValue,
POWER(CONVERT(NUMERIC(38,0), 256), n.Num-1) AS Byte,
CONVERT(INT, SUBSTRING(@HexString, n.Num, 1)) * POWER(CONVERT(NUMERIC(38,0), 256), n.Num-1) AS IntValueMultipliedByte
FROM N10 n
WHERE n.Num <= LEN(@HexString)
) src;
GO
/*
NumHexValue IntValue Byte IntValueMultipliedByte SumOverAll
----------- ----------- ---------- ---------------------- ------------
1 0xE9F6 233 1 233 123456780009
2 0xF698 246 256 62976 123456780009
3 0x98BE 152 65536 9961472 123456780009
4 0xBE1C 190 16777216 3187671040 123456780009
5 0x1C 28 4294967296 120259084288 123456780009
*/
注意:我使用的是 SQL2008R2。