Skip to content
Advertisement

(SQL Server 2017) I get error Msg 8152 and I don’t know why

I have no idea why, but I am getting the error Msg 8152 (in SQL Server 2017) after trying to insert something like

INSERT INTO Users (usr_name, usr_pwd, usr_thing1, usr_thing2)
VALUES ('ticko', 'kgrhjwekr3h4', 1, '34798563');

into the table

CREATE TABLE Users
(
    usr_id INTEGER NOT NULL PRIMARY KEY IDENTITY(0, 1),
    usr_name VARCHAR NOT NULL,
    usr_pwd VARCHAR NOT NULL,
    usr_thing1 INT NOT NULL,
    usr_thing2 VARCHAR NOT NULL,
    usr_group INT,
    usr_status VARCHAR DEFAULT 'ACTIVE',
    usr_created DATE DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (usr_group)
        REFERENCES Groups(grp_reqid)
);

The output I get is

Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.

Thanks in advance for your help!

Advertisement

Answer

You need to define a length for your VARCHAR columns

For example

CREATE TABLE Users(
    usr_id INTEGER NOT NULL PRIMARY KEY IDENTITY(0, 1),
    usr_name VARCHAR(100) NOT NULL,
    usr_pwd VARCHAR(100) NOT NULL,
    usr_thing1 INT NOT NULL,
    usr_thing2 VARCHAR(100) NOT NULL,
    usr_group INT,
    usr_status VARCHAR(100) DEFAULT 'ACTIVE',
    usr_created DATE DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (usr_group)
        REFERENCES Groups(grp_reqid)
);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement