S
S
Sergey2021-09-29 19:09:15
SQL
Sergey, 2021-09-29 19:09:15

How to remove The INSERT statement conflicted with the CHECK constraint error?

Good day!
I get the following error when I try to add a new record to the database:

The INSERT statement conflicted with the CHECK constraint "CK_emp_id". The conflict occurred in database "pubs", table "dbo.employee", column 'emp_id'.
The statement has been terminated.


The constraint itself looks like this:
([emp_id] like '[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][FM]' OR [emp_id] like '[A-Z]-[A-Z][1-9][0-9][0-9][0-9][0-9][FM]')


C# code:
SqlParameter pEmpIdIns = new SqlParameter("@EmpId", empId); 
pEmpIdIns.SqlDbType = SqlDbType.Char;


Stored procedure:
CREATE OR ALTER PROCEDURE spInsertEmployee
@empId CHAR,
@fName VARCHAR(20),
@mInit CHAR(1)=NULL,
@lName VARCHAR(30),
@jobId SMALLINT,
@jobLvl TINYINT,
@pubId CHAR(4),
@hireDate DATETIME

AS

BEGIN
    INSERT INTO employee (emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date, EmployeeType)
                    VALUES(@empId, @fName, @mInit, @lName, @jobId, @jobLvl, @pubId, @hireDate, 0);
END
GO


I pass the value from the form: "A-A11111F" (or any other)

Hands in if you execute sql
INSERT INTO employee (emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date, EmployeeType)
VALUES('A-A11111F', 'Test', 'T', 'Tester', 14, 90, 9952, 2021-01-01, 0);

then everything is added.

How and what needs to be fixed? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-09-29
@freeExec

Obviously, what goes to the server is not at all what you insert with your hands.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question