SQL Server - 如何在不删除的情况下更改列 nvarchar 长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45845430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Server - How to alter column nvarchar length without drop
提问by Mário Tomé
I'm trying to alter the length of the column "Body" in table "Post" like this:
我正在尝试像这样更改“Post”表中“Body”列的长度:
ALTER TABLE Post ALTER COLUMN Body nvarchar(8000) NOT NULL;
The column is defined as nvarchar(4000) and it gives me this error:
该列被定义为 nvarchar(4000) 并且它给了我这个错误:
Msg 2717, Level 16, State 2, Line 1 The size (8000) given to the parameter 'Body' exceeds the maximum allowed (4000).
消息 2717,级别 16,状态 2,第 1 行 指定给参数“Body”的大小 (8000) 超过了允许的最大值 (4000)。
回答by UnhandledExcepSean
Use Max instead. If the column were declared as VARCHAR, 8000 would be ok.
请改用 Max。如果将列声明为 VARCHAR,则 8000 就可以了。
ALTER TABLE Post ALTER COLUMN Body nvarchar(max) NOT NULL;
回答by Chandra Sekhar Kosuru
In case, if the column has any constraint key like default, check .. ext, First we have dropped the key constraint from the column than alter the size of the column and alter your constraint to the column. The below steps will help you.
如果列有任何约束键,如默认,请检查 .. ext,首先我们从列中删除键约束,然后更改列的大小并更改对列的约束。以下步骤将帮助您。
Steps are,
步骤是,
> ALTER TABLE MESSAGE_INBOX_VIEWERS DROP CONSTRAINT DF_CONST_TEXT
> ALTER TABLE MESSAGE_INBOX_VIEWERS ALTER COLUMN TEXT NVARCHAR(MAX)
> ALTER TABLE MESSAGE_INBOX_VIEWERS ADD CONSTRAINT DF_CONST_TEXT DEFAULT('') FOR TEXT