简体   繁体   中英

The INSERT statement conflicted with the CHECK constraint

Can any body give the suggestions.Why I am getting this error.

System.Data.SqlClient.SqlException: The INSERT statement conflicted with the CHECK constraint "CK_ GL10000 _TRXDATE__56701F76". The conflict occurred in database "TWO", table "dbo.GL10000", column 'TRXDATE'.

This is my stored procedure :

SqlCommand myCommand1 = new SqlCommand("taGLTransactionHeaderInsert", strcon);

  myCommand1.CommandType = CommandType.StoredProcedure;

  myCommand1.CommandText = "taGLTransactionHeaderInsert";

  myCommand1.Parameters.Add("@I_vBACHNUMB", SqlDbType.Char).Value = GLHdr.BACHNUMB;

  myCommand1.Parameters.Add("@I_vREFRENCE", SqlDbType.Char).Value = "ExcelImport";

  myCommand1.Parameters.Add("@I_vTRXDATE", SqlDbType.DateTime).Value = GLHdr.TRXDATE;

Table definition:

CREATE TABLE [dbo].[GL10000](

[PSTGSTUS] [smallint] NOT NULL,

[LASTUSER] [char](15) NOT NULL,

[LSTDTEDT] [datetime] NOT NULL,

[USWHPSTD] [char](15) NOT NULL,

[TRXTYPE] [smallint] NOT NULL

    /* More columns? not included in script that OP added */
)
GO

ALTER TABLE [dbo].[GL10000]  WITH CHECK ADD CHECK
  ((datepart(hour,[RVRSNGDT])=(0) AND datepart(minute,[RVRSNGDT])=(0)
  AND datepart(second,[RVRSNGDT])=(0) AND datepart(millisecond,[RVRSNGDT])=(0)))
GO

ALTER TABLE [dbo].[GL10000]  WITH CHECK ADD CHECK
  ((datepart(hour,[Tax_Date])=(0) AND datepart(minute,[Tax_Date])=(0)
  AND datepart(second,[Tax_Date])=(0) AND datepart(millisecond,[Tax_Date])=(0)))
GO

ALTER TABLE [dbo].[GL10000]  WITH CHECK ADD CHECK
  ((datepart(day,[TIME1])=(1) AND datepart(month,[TIME1])=(1)
  AND datepart(year,[TIME1])=(1900)))
GO

ALTER TABLE [dbo].[GL10000]  WITH CHECK ADD CHECK
  ((datepart(hour,[TRXDATE])=(0) AND datepart(minute,[TRXDATE])=(0)
  AND datepart(second,[TRXDATE])=(0) AND datepart(millisecond,[TRXDATE])=(0)))
GO

ALTER TABLE [dbo].[GL10000] ADD  CONSTRAINT
  [DF__GL10000__DEX_ROW__540C7B00]  DEFAULT (getutcdate()) FOR [DEX_ROW_TS]
GO

In the stored procedure 'taGLTransactionHeaderInsert', add the following line somwhere near the top, prior to the insert.

set @I_vTRXDATE = convert(varchar, @I_vTRXDATE, 101)

In SQL 2008, you can also use the DATE data type.

create proc taGLTransactionHeaderInsert
{
    @I_vTRXDATE DATE
}

And CAST as DATE

DECLARE @I_vTRXDATE2 DATE
SET @I_vTRXDATE2 = CAST(@I_vTRXDATE as DATE)

This will remove the time portion of whatever datetime value is being passed in. However, if passing in date + time is incorrect, then the error is likely to be expected and you should correct the value being passed into the stored procedure running any business logic rules necessary to satisfy the requirements.

You could also truncate the value being passed in, ie

GLHdr.TRXDATE.Date;

Also note that in SQL 2008, the DATE data type was introduced. You can read about it here .

Personally, I would remove the constraint. TRXDATE sounds like it stands for "transaction date". Generally, one would want to know the exact date and time that a transaction occurred. For presentation purposes, if you only want the DATE to be displayed, then simply truncate the time but your data would be completely intact.

There is a CHECK constraint on the table, and the value you are passing to the stored procedure either doesn't match the check constraint, or isn't the value that's trying to make it as far as the TRXDATE column in the GL10000 table. Your easiest bet is to use SSMS to explore this table and find the CHECK constraint.

It has been a while since this question was arisen, I got a similar error, so did this to find out what was exactly the rule in CHECK : From SQL server management (my current version 2012), click on the table that is causing the error, then expand the Constraints folder,
right click on the problematic Constraints and select the " Modify ", this will show the definition of constraint, you can easily find the CHECK part in the definition, for my case it was checking if my column is getting only character of 'C' or 'O' for my the column, while in the code, I was trying to insert a character of 'E'.

Constraint definition:

    CHECK  (([ColumnName]='C' OR [ColumnName]='O'))

My code:

         switch (item.Type)
           {
            case myType.Elective:
                entityName.ColumnName = "E";
                .
                .
                .
            }

Based on this CHECK part, you can modify your code to full fill the check condition, or if the check condition is not right, just modify that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM