SQL Server How can I resolve an INSERT statement conflict with a CHECK constraint in SQL? [closed]

aiqt4smr  于 2023-06-04  发布在  其他
关注(0)|答案(1)|浏览(172)

Closed. This question needs debugging details . It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem . This will help others answer the question.

Closed 3 days ago.
Improve this question

I get this error:

Msg 547, Level 16, State 0, Line 105
The INSERT statement conflicted with the CHECK constraint "CK__Orders__Order_St__33D4B598". The conflict occurred in database "Online_Sales", table "dbo.Orders", column 'Order_Status'.

I tried inserting data into a table in SQL Server, when I got this error.

2ul0zpep

2ul0zpep1#

There is a check constraint defined on the table that, essentially, says "you can't insert data unless you pass these rules." You need to determine what those rules (the check constraint's definition) are, and make sure your data can pass that test.

There are ways to drill down in SSMS to find these rules. Another alternative is to query the system tables, which is what I generally do--but only because I have access to a bunch of support scripts, accumulated over the years. Below is one that should help you find the code in question.

SELECT
   sc.name   [Schema (CKs)]
  ,taP.name  [Table]
  ,cc.name   CheckName
  ,case when col.name is null then '...' else col.name end  [Col or Tbl]
  ,cc.is_disabled
  ,cc.is_not_trusted
  ,cc.definition
 from sys.check_constraints  cc
  inner join sys.tables  taP
   on taP.object_id = cc.parent_object_id
  inner join sys.schemas sc
   on sc.schema_id = taP.schema_id
  left outer join sys.columns  col
   on col.object_id = taP.object_id
    and col.column_id = cc.parent_column_id
 --where sc.name = '??'
 -- and taP.Name = '??'

相关问题