【发布时间】:2011-09-30 13:52:54
【问题描述】:
在过去的任何时候,如果有人问我 varchar(max) 的最大大小,我会说 2GB,或者查找更准确的 figure(2^31-1,或 2147483647) .
但是,在最近的一些测试中,我发现varchar(max) 变量显然可以超过这个大小:
create table T (
Val1 varchar(max) not null
)
go
declare @KMsg varchar(max) = REPLICATE('a',1024);
declare @MMsg varchar(max) = REPLICATE(@KMsg,1024);
declare @GMsg varchar(max) = REPLICATE(@MMsg,1024);
declare @GGMMsg varchar(max) = @GMsg + @GMsg + @MMsg;
select LEN(@GGMMsg)
insert into T(Val1) select @GGMMsg
select LEN(Val1) from T
结果:
(no column name)
2148532224
(1 row(s) affected)
Msg 7119, Level 16, State 1, Line 6
Attempting to grow LOB beyond maximum allowed size of 2147483647 bytes.
The statement has been terminated.
(no column name)
(0 row(s) affected)
所以,鉴于我现在知道 变量 可以超过 2GB 的障碍 - 有谁知道 varchar(max) 变量的实际限制是多少?
(以上测试在 SQL Server 2008(不是 R2)上完成。我很想知道它是否适用于其他版本)
【问题讨论】:
-
declare @x varchar(max) = 'XX'; SELECT LEN(REPLICATE(@x,2147483647))为我提供了4294967294,但需要很长时间才能运行 - 即使在SELECT已返回之后,所以不确定额外的时间花在做什么上。
标签: sql-server tsql