Sql 字符串或二进制数据将被截断。声明已终止。简单错误
我得到了这个错误Sql 字符串或二进制数据将被截断。声明已终止。简单错误,sql,sql-server,Sql,Sql Server,我得到了这个错误 Create Table rs ( Id int IDENTITY (1,1) Primary Key, mId int Not NUll, ad varchar Not NUll, stvarchar Not NUll, et varchar Not NUll, nt varchar(max) ); insert into rs ( nt, et, st, ad, mId) values ('as','as'
Create Table rs
(
Id int IDENTITY (1,1) Primary Key,
mId int Not NUll,
ad varchar Not NUll,
stvarchar Not NUll,
et varchar Not NUll,
nt varchar(max)
);
insert into rs ( nt, et, st, ad, mId)
values ('as','as','as','as',12)
这是一个简单的sql,但在解决它时发现困难您没有为
varchar
变量设置大小,因此大小将默认为1,因此在将大小为2的varchar
插入表时出现此错误,请为varchar
变量提供大小以解决此问题
Msg 8152, Level 16, State 14, Line 2
String or binary data would be truncated.
The statement has been terminated.
发件人:
varchar[(n|max)]可变长度、非Unicode字符串数据。n定义字符串长度,可以是1到8000之间的值。max表示最大存储大小为2^31-1字节(2 GB) 备注 当数据定义或变量声明语句中未指定n时,默认长度为1。如果在使用强制转换和转换函数时未指定n,则默认长度为30 为
Varchar
指定默认长度。同时查看此项
正如@Krish指出的,您需要分配空间,即为每个列定义大小
Create Table rs
(
Id int IDENTITY (1,1) Primary Key,
mId int Not NUll,
ad varchar(5) Not NUll,
st varchar(5) Not NUll,
et varchar(5) Not NUll,
nt varchar(max)
);
insert into rs ( nt, et, st, ad, mId)
values ('as','as','as','as',12
或
默认大小为1,因此这也可以使用。。。但这不是你想要的(只是为了解释问题)
可能的副本:
Create Table rs
(
Id int IDENTITY (1,1) Primary Key,
mId int Not NUll,
ad varchar(5) Not NUll,
st varchar(5) Not NUll,
et varchar(5) Not NUll,
nt varchar(max)
);
insert into rs ( nt, et, st, ad, mId)
values ('as','as','as','as',12
Create Table rs
(
Id int IDENTITY (1,1) Primary Key,
mId int Not NUll,
ad varchar(2) Not NUll,
st varchar(2) Not NUll,
et varchar(2) Not NUll,
nt varchar(max)
);
insert into rs ( nt, et, st, ad, mId)
values ('as','as','as','as',12)
INSERT INTO #rs ( mId, ad, st, et, nt)
VALUES (12, 'a','a', 'a', 'a')