如何是「錢」 [...]在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。
它是**不**存儲在一個字符串(字符)形式。請確認你知道這一點。 –
你關心內存存儲而不是數據文件嗎? – StingyJack
是的!我知道它沒有存儲在char中。 – Nooshin