将表达式转换为数据类型int的算术溢出错误,如何解决?

时间:2018-08-30 06:58:02

标签: sql sql-server tsql

这是创建表的代码。

CREATE TABLE CUSTOMERS
(
    Customer_ID INT NOT NULL,
    CHECK(Customer_ID <= 11),
    First_Name varchar(20) NOT NULL,
    Last_Name varchar(30),
    Home_Street varchar(30),
    Home_City varchar(20),
    Home_State varchar(2),
    Home_Zip varchar(5),
    PhoneNumber varchar(11) NOT NULL
);

ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS PRIMARY KEY(Customer_ID);

然后我尝试将数据(使用此代码)插入表中,这就是我收到此错误的地方。

INSERT INTO dbo.CUSTOMERS(Customer_ID, First_Name, Last_Name, Home_Street, Home_City, Home_State, Home_Zip, PhoneNumber)
VALUES (11223344556, 'John', 'Doe', '1234 Hand Street', 'Wahiawa', 'HI', 96786, 2535551267);

我在做什么错了,我该怎么做才能解决此问题?

3 个答案:

答案 0 :(得分:3)

据我所知,您正在检查Customer_ID <=11的长度,因此您应该提及len(Customer_ID)<=11可以使用,并且应该将Customer_ID int的数据类型更改为bigint

CREATE TABLE CUSTOMERS
    (
    Customer_ID bigINT NOT NULL,
    CHECK(len(Customer_ID)<=11),
    First_Name varchar(20) NOT NULL,
    Last_Name varchar(30),
    Home_Street varchar(30),
    Home_City varchar(20),
    Home_State varchar(2),
    Home_Zip varchar(5),
    PhoneNumber varchar(11) NOT NULL
    );
    ALTER TABLE CUSTOMERS
    ADD CONSTRAINT PK_CUSTOMERS
    PRIMARY KEY(Customer_ID);

    INSERT INTO dbo.CUSTOMERS(Customer_ID,First_Name,Last_Name,Home_Street,
    Home_City,Home_State,Home_Zip,PhoneNumber)
    VALUES(11223344556,'John','Doe','1234 Hand Street', 
    'Wahiawa','HI',96786,2535551267);

答案 1 :(得分:1)

您需要客户ID为 bigint:

CREATE TABLE CUSTOMERS
(
Customer_ID BIGINT NOT NULL,
CHECK(Customer_ID<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);

答案 2 :(得分:1)

该问题可能是由于CHECK(Customer_ID <= 11),因为Customer_ID id整数数据类型服务器可能会检查整数验证而不是长度验证。尝试更改验证。