我可以为 varbinary 的最大大小设置 2 MB 吗? [英] Can I set 2 MB for maximum size of varbinary?
问题描述
据我所知,您可以手动"定义的最大值是 8000
-> varbinary(8000)
,据我所知,这意味着 8000 字节
-> 7,8125 KB
.
As far as I know the maximum value you can define "manually" is 8000
-> varbinary(8000)
which, as far as I know, means 8000 bytes
-> 7,8125 KByte
.
是否可以将最大值设置为 2 MB
?类似于 varbinary(2097152)
,还是我应该将其设置为 varbinary(max)
并通过我的上传/sql 插入脚本检查文件大小?
Is it possible to set max to 2 MB
? Something ike varbinary(2097152)
, or shall I set it to varbinary(max)
and check the file size through my upload/sql insert script?
推荐答案
您可以使用 CHECK CONSTRAINT
来确保大小低于 2MB:
You could use a CHECK CONSTRAINT
to ensure the size is below 2MB:
CREATE TABLE dbo.T
(
ID INT IDENTITY,
VarB VARBINARY(MAX)
);
ALTER TABLE dbo.T ADD CONSTRAINT CHK_T_VarB__2MB CHECK (DATALENGTH(VarB) <= 2097152);
然后尝试插入大于 2 MB 的内容时:
Then when trying to insert something larger than 2 MB:
DECLARE @B VARCHAR(MAX) = '|';
INSERT dbo.T (VarB)
SELECT CONVERT(VARBINARY(MAX), REPLICATE(@B, 2097153));
出现错误:
INSERT 语句与 CHECK 约束CHK_T_Column__2MB"冲突.数据库TestDB"、表dbo.T"、VarB"列发生冲突.
The INSERT statement conflicted with the CHECK constraint "CHK_T_Column__2MB". The conflict occurred in database "TestDB", table "dbo.T", column 'VarB'.
这篇关于我可以为 varbinary 的最大大小设置 2 MB 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!