[英]When is nvarchar(Max) = nvarchar(4000)?
There are multiple posts on both SO and other sites which clearly state that the maximum length of nvarchar(max)
is 2GB. SO和其他站点上有多个帖子,清楚地表明nvarchar(max)
的最大长度为2GB。 However, I see also much confusion in both internet and real life that it actually is 8000/4000 in Unicode. 但是,我在互联网和现实生活中也看到很多混乱,它实际上是Unicode中的8000/4000。
I would like to know what things could change that fact, or maybe lead someone to falsely assume so. 我想知道什么事情可以改变这个事实,或者可能导致某人错误地假设。
Some suggestions/partial answers I've already gathered: 我已经收集了一些建议/部分答案:
When assigning nvarchar(max)
variable/column to a concatenation of non-max-sized components, must we convert everything to nvarchar(max)
explicitly? 将nvarchar(max)
变量/列分配给非最大大小的组件的串联时,我们必须将所有内容明确地转换为nvarchar(max)
吗? Here is something showcasing a strange example, where a text-returning function requires converting, whereas the N for the literal can be omitted: 这里展示了一个奇怪的例子,其中文本返回函数需要转换,而文字的N可以省略:
declare @s nvarchar(max) select @s = convert(nvarchar(max), replicate('.', 8000)) + N'Hi!' select len(@s) -- returns 8003 declare @s nvarchar(max) select @s = replicate('.', 8000) + N'Hi!' select len(@s) -- returns 4000 declare @s nvarchar(max) select @s = convert(nvarchar(max), replicate('.', 8000)) + 'Hi!' select len(@s) -- returns 8003
Are there ways to disable the functionality? 有没有办法禁用该功能? Does sp_tableoption @OptionName=large value types out of row
or OBJECTPROPERTY(id,'TableTextInRowLimit')
have anything to do with this? sp_tableoption @OptionName=large value types out of row
或OBJECTPROPERTY(id,'TableTextInRowLimit')
是否与此有关?
Clarification : My aim is not to use this functionality, but be aware of its existence, which may be indeed have been used by a higher privilege user that will prevent me from using the max size. 澄清 :我的目标不是使用此功能,而是要注意它的存在,这可能确实已被更高权限的用户使用,这将阻止我使用最大大小。
Any other points gladly welcome 其他任何一点都欢迎
A few points here, as I can't fit into a comment. 这里有几点,因为我不能适应评论。
(n)varchar(MAX)
was introduced in SQL Server 2005 . (n)varchar(MAX)
是在SQL Server 2005中引入的 。 Previously you had to make use of text
, ntext
and image
for varchar(MAX)
, nvarchar(MAX)
and varbinary(MAX)
. 以前你必须使用varchar(MAX)
, nvarchar(MAX)
和varbinary(MAX)
的text
, ntext
和image
。 The old data type have been deprecated for a long time now and you should not be using them. 旧数据类型已被弃用了很长时间,您不应该使用它们。 varchar(10)
and a varchar(100)
concatenated would return a varchar(110)
. Note, however, that to achieve the usage of the MAX
length, at least one string must be an (n)varchar(MAX)
. SELECT REPLICATE(N'A',3000) + REPLICATE(N'A',3000) AS S
would return a 4000 character string. + (String Concatenation) (Transact-SQL) - Remarks : 当涉及长度时,使用长度的组合值(连接的varchar(10)
和varchar(100)
将返回varchar(110)
。但是,请注意,为了实现MAX
长度的使用,至少一个字符串必须是(n)varchar(MAX)
SELECT REPLICATE(N'A',3000) + REPLICATE(N'A',3000) AS S
将返回4000个字符串。 +(String Concatenation)(Transact- SQL) - 备注 :
If the result of the concatenation of strings exceeds the limit of 8,000 bytes, the result is truncated. 如果字符串串联的结果超过8,000字节的限制,则结果将被截断。 However, if at least one of the strings concatenated is a large value type, truncation does not occur. 但是,如果连接的至少一个字符串是大值类型,则不会发生截断。
(n)varchar(MAX)
? (n)varchar(MAX)
的用法? Why? 为什么? If you wanted to stop people using a data type stop them using (n)text
and image
. 如果您想阻止使用数据类型的人使用(n)text
和image
停止它们。 In all seriousness though, you can't stop the usage of a data type. 但是,严肃地说,您无法停止使用数据类型。 Perhaps you could get "clever" with DDL triggers, but I advise against it. 也许你可以用DDL触发器“聪明”,但我建议反对它。 sp_tableoption
cannot be used to stop someone using a MAX
length datatype no; 要回答编辑, sp_tableoption
不能用于阻止某人使用MAX
length数据类型no; my above point stands. 我的上述观点。 To quote the documetation ( sp_tableoption (Transact-SQL) - Arguments : 引用文档( sp_tableoption(Transact-SQL) - 参数 :
Large value types out of row: 超行的大值类型:
1 =varchar(max)
,nvarchar(max)
,varbinary(max)
,xml
and large user-defined type (UDT) columns in the table are stored out of row, with a 16-byte pointer to the root. 1 =varchar(max)
,nvarchar(max)
,varbinary(max)
,xml
和表中的大型用户定义类型(UDT)列存储在行外,并带有指向根的16字节指针。
0 =varchar(max)
,nvarchar(max)
,varbinary(max)
,xml
and large UDT values are stored directly in the data row, up to a limit of 8000 bytes and as long as the value can fit in the record. 0 =varchar(max)
,nvarchar(max)
,varbinary(max)
,xml
和大UDT值直接存储在数据行中,varbinary(max)
限制为8000字节,只要该值可以适合记录。 If the value does not fit in the record, a pointer is stored in-row and the rest is stored out of row in the LOB storage space. 如果该值不适合记录,则指针存储在行中,其余指针存储在LOB存储空间中的行之外。 0 is the default value. 0是默认值。
Large user-defined type (UDT) applies to: SQL Server 2008 through SQL Server 2017. 大型用户定义类型(UDT)适用于:SQL Server 2008到SQL Server 2017。
Use theTEXTIMAGE_ON
option ofCREATE TABLE
to specify a location for storage of large data types. 使用CREATE TABLE
的TEXTIMAGE_ON
选项指定存储大型数据类型的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.