简体   繁体   English

什么时候是nvarchar(Max)= nvarchar(4000)?

[英]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: 我已经收集了一些建议/部分答案:

  1. Are there older SQL Server versions which did only support a maximum of 4000? 是否有较旧的SQL Server版本,最多只支持4000?
  2. 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 
  3. 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 rowOBJECTPROPERTY(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. 澄清 :我的目标不是使用此功能,而是要注意它的存在,这可能确实已被更高权限的用户使用,这将阻止使用最大大小。

  4. Any other points gladly welcome 其他任何一点都欢迎

A few points here, as I can't fit into a comment. 这里有几点,因为我不能适应评论。

  1. Yes. 是。 (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)textntextimage The old data type have been deprecated for a long time now and you should not be using them. 旧数据类型已被弃用了很长时间,您不应该使用它们。
  2. When combining data, data type precedence is used to work out the final data type. 组合数据时,使用数据类型优先级来计算最终数据类型。 When lengths are involved, the combined values of the lengths are used (A 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. 但是,如果连接的至少一个字符串是大值类型,则不会发生截断。

  3. Disable what functionality? 禁用什么功能? The usage of (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)textimage停止它们。 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触发器“聪明”,但我建议反对它。

    To answer the edit, 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 the TEXTIMAGE_ON option of CREATE TABLE to specify a location for storage of large data types. 使用CREATE TABLETEXTIMAGE_ON选项指定存储大型数据类型的位置。

  4. Too broad for SO. SO太宽泛了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将nvarchar(4000)转换为nvarchar(max) - Converting nvarchar(4000) to nvarchar(max) 将列从 Nvarchar(max) 更改为 Nvarchar(4000) 错误 - Changing columns from Nvarchar(max) to Nvarchar(4000) error Nvarchar(max)在SQL Server中缩小到4000个字符 - Nvarchar(max) shrinks to 4000 chars in SQL Server 对nvarchar(4000)的性能有影响吗? - Performance implication on nvarchar(4000)? 对于Nvarchar(Max),我只在TSQL中获得4000个字符? - For Nvarchar(Max) I am only getting 4000 characters in TSQL? 存储过程变量定义的nvarchar(max)被截断为4000个字符 - Stored procedure variable defined nvarchar(max) gets truncated to 4000 chars 参数化查询'((@id nvarchar(4000),@ name nvarchar(4000),@ project nvarchar(4000)')期望参数'@id',未提供 - The parameterized query '(@id nvarchar(4000),@name nvarchar(4000),@project nvarchar(4000)' expects the parameter '@id', which was not supplied MSSQL:作为文本输出时截断 nvarchar(max) - MSSQL: Truncate nvarchar(max) when outputted as text 使用nvarchar(max)时文本溢出? - Text overflow when using nvarchar(max)? 使用Dapper获取nvarchar(max)会返回一个修剪为4000个字符的字符串。这种行为可以改变吗? - Using Dapper to get nvarchar(max) returns a string trimmed to 4000 characters. Can this behaviour be changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM