过去的任何时候,如果有人问我varchar(max)
的最大大小,我会说是2GB,或者查找更精确的数字(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)上完成。我很想知道它是否适用于其他版本)
4294967294
,但运行时间很长 - 即使在SELECT
已经返回后仍然需要很长时间,所以不确定额外的时间花费在做什么。 - Martin Smith