【问题标题】:Cannot insert the value null error but value isn't actually null无法插入值 null 错误,但值实际上不是 null
【发布时间】:2014-08-19 22:32:23
【问题描述】:

我有一个似乎很容易解决的错误,但我没能做到。当我运行一条语句将一条记录插入表中时,我收到以下错误:

Cannot insert the value NULL into column 'IsFutureDateAllowForDataSubmission', table 'TableA'; column does not allow nulls. INSERT fails.

很简单,对吧?我正在尝试将 null 插入到不允许它的列中。然而,在 INSERT 语句中我明确地为该列插入了一个值。没有什么花哨的,没有动态 SQL,没有子查询或类似的东西。我有一个简单的陈述,列举了每个值。由于上述错误而失败

TableA 由 81 列组成。它是大型 Web 应用程序的配置表。该列不允许空值。列数据类型是位(应用程序的 TRUE/FALSE 标志)。我无法更改桌子的设计。

IsFutureDateAllowForDataSubmission 定义:

+------------------------------------+-----------+-----------+-----------+--------+-------+-------+----------+---------+------------+---------------+------------------+-------------------+------------------+-------------+--------------+
|            COLUMN_NAME             | DATA_TYPE | TYPE_NAME | PRECISION | LENGTH | SCALE | RADIX | NULLABLE | REMARKS | COLUMN_DEF | SQL_DATA_TYPE | SQL_DATETIME_SUB | CHAR_OCTET_LENGTH | ORDINAL_POSITION | IS_NULLABLE | SS_DATA_TYPE |
+------------------------------------+-----------+-----------+-----------+--------+-------+-------+----------+---------+------------+---------------+------------------+-------------------+------------------+-------------+--------------+
| IsFutureDateAllowForDataSubmission |        -7 | bit       |         1 |      1 | NULL  | NULL  |        0 | NULL    | ((0))      |            -7 | NULL             | NULL              |               63 | NO          |           50 |
+------------------------------------+-----------+-----------+-----------+--------+-------+-------+----------+---------+------------+---------------+------------------+-------------------+------------------+-------------+--------------+

我将0 传递给IsFutureDateAllowForDataSubmission,我也尝试了'0'。两者都会导致相同的错误。

到目前为止,除了人们尝试在设置为标识的列中添加值之外,我还没有在网上找到太多内容,但是这种情况在这里不相关,因为我尝试添加值的列是一个简单的 0 /1 标志,不是身份。

如果需要,我很乐意提供更多信息。实际的插入语句很长(我明确为其赋值的 81 列)。

更新

这里是表(TableA)的定义:

+-------------------------------------------------+----------+----------+--------+-------+-------+----------+--------------------+----------------------+------------------------------+
|                   Column_name                   |   Type   | Computed | Length | Prec  | Scale | Nullable | TrimTrailingBlanks | FixedLenNullInSource |          Collation           |
+-------------------------------------------------+----------+----------+--------+-------+-------+----------+--------------------+----------------------+------------------------------+
| BUSNS_UNIT_ID                                   | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| BUSNS_UNIT_NM                                   | nvarchar | no       |     40 |       |       | yes      | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| ALLOW_RPG_EDIT_FLAG                             | bit      | no       |      1 |       |       | yes      | (n/a)              | (n/a)                | NULL                         |
| FISCAL_YR_START_MTH                             | tinyint  | no       |      1 |     3 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| INPUT_FORM_EXCEL_FLAG                           | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| SHOW_COMPOSITE_CALCULATION_TYPE                 | bit      | no       |      1 |       |       | yes      | (n/a)              | (n/a)                | NULL                         |
| DEFAULT_COMPOSITE_CALCULATION_TYPE              | nvarchar | no       |    200 |       |       | yes      | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| MAP_ALL_DEPT_FLAG                               | bit      | no       |      1 |       |       | yes      | (n/a)              | (n/a)                | NULL                         |
| COMPST_MIN_DLRS_TO_RUN                          | int      | no       |      4 |    10 |     0 | yes      | (n/a)              | (n/a)                | NULL                         |
| ACCT_DATA_TYP                                   | varchar  | no       |     10 |       |       | yes      | no                 | yes                  | SQL_Latin1_General_CP1_CI_AS |
| DECIMAL_SEPARATOR_DEFAULT                       | varchar  | no       |     10 |       |       | yes      | no                 | yes                  | SQL_Latin1_General_CP1_CI_AS |
| FORM_READONLY_FLAG                              | bit      | no       |      1 |       |       | yes      | (n/a)              | (n/a)                | NULL                         |
| CMPST_RUN_MTHS                                  | int      | no       |      4 |    10 |     0 | yes      | (n/a)              | (n/a)                | NULL                         |
| CMPST_RUN_YRS                                   | int      | no       |      4 |    10 |     0 | yes      | (n/a)              | (n/a)                | NULL                         |
| DISPLAY_LCL_ACCT_NBR                            | bit      | no       |      1 |       |       | yes      | (n/a)              | (n/a)                | NULL                         |
| csv_delimiter                                   | varchar  | no       |      2 |       |       | no       | no                 | no                   | SQL_Latin1_General_CP1_CI_AS |
| SHOW_PLANS                                      | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| INPUT_FORM_TEMPLATE_NAME                        | nvarchar | no       |    300 |       |       | yes      | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| COMPST_RUN_REVIEWSTATUS_DEFAULT                 | tinyint  | no       |      1 |     3 |     0 | yes      | (n/a)              | (n/a)                | NULL                         |
| AllowDuplicateAccountData                       | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| FILE_IMPORT_SCHEMA                              | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| ALLOW_DELETE_COMPOSITE                          | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| Disable_Multiple_StarMap                        | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| AllowMasterAccountMap                           | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ShowCompositesToVendors                         | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| AllowCompositeClosedSnapShot                    | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| SubmissionFrequencyID                           | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| AutoRecalculate                                 | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ShowDealerCodeAlternate                         | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ShowSubDealerIndicator                          | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ShowDealerType                                  | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| OnlineBusinessPlan                              | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ShowInactiveItemsAdvancedCriteria               | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| HonorRollType                                   | nvarchar | no       |    200 |       |       | no       | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| ShowInactiveDealersDataSubmission               | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| AllowFinancialDataSuppression                   | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| AllowDataSubmissionComments                     | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| RejectMatchingSubmissions                       | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| HonorRollAccuracyPoint                          | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| HonorRollNonSubmissionPoint                     | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| DisableInputFormAccountHighlight                | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| DisableInputFormAuditComments                   | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| EnableSubDealers                                | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ShowCompositesInDealerProfile                   | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| EnableCarryForwardAuditRemark                   | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| EnableInputFormInlineEvents                     | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| PreventOverMap                                  | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| DefaultAdhocReportVariablesTab                  | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| WorkingCapitalFormula                           | nvarchar | no       |     -1 |       |       | no       | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| InvestmentGuideFormula                          | nvarchar | no       |     -1 |       |       | no       | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| DualDealerAppointmentMonthCutoff                | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| WorkingCapitalSubmissionsStartMonth             | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| WorkingCapitalSubmissionsEndMonth               | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| WorkingCapitalSuccessfulSubmissionsMinimum      | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| WorkingCapitalStartMonth                        | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| WorkingCapitalEndMonth                          | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| WorkingCapitalActivationMonthCutoff             | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| AdhocShowAllUserReports                         | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| AuditResultsLinkStatus                          | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| ShowHistoricCompositesEntitySearch              | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| WorkSheetFullScreenMode                         | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| OnlineBusinessForecast                          | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| IsFutureDateAllowForDataSubmission              | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| SpecialCarryForward                             | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| DmsFileTransferFormat                           | nvarchar | no       |     -1 |       |       | yes      | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| SiteLogo                                        | varchar  | no       |     -1 |       |       | yes      | no                 | yes                  | SQL_Latin1_General_CP1_CI_AS |
| SubmissionPointsThreshold                       | int      | no       |      4 |    10 |     0 | no       | (n/a)              | (n/a)                | NULL                         |
| EnableCompositeAverageByDepartment              | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ViewPreviousSubmission                          | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| HideAuditResultZeroValues                       | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| KeepReleasedFileImportData                      | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ArlAccountNumberParse                           | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| ConvertForecastRoles                            | nvarchar | no       |     -1 |       |       | yes      | (n/a)              | (n/a)                | SQL_Latin1_General_CP1_CI_AS |
| ArlEnableUndo                                   | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| UnhideAuditResultSummaryAccountZeroValues       | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| AlwaysShowAuditResultSummaryAccountCarryForward | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| CompositeAverageSummaryAccounts                 | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| DynamicPortfolio                                | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| EnableAllocationAccounts                        | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| EnableTermsAndConditions                        | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
| EnableSimultaneousAudit                         | bit      | no       |      1 |       |       | no       | (n/a)              | (n/a)                | NULL                         |
+-------------------------------------------------+----------+----------+--------+-------+-------+----------+--------------------+----------------------+------------------------------+

这是生成错误的完整 INSERT 语句:

INSERT INTO TableA SELECT
'14' AS BUSNS_UNIT_ID,
'Global' AS  BUSNS_UNIT_NM,
'0' AS  ALLOW_RPG_EDIT_FLAG,
'1' AS  FISCAL_YR_START_MTH,
'1' AS  INPUT_FORM_EXCEL_FLAG,
'0' AS  SHOW_COMPOSITE_CALCULATION_TYPE,
'ANNUALIZATION' AS  DEFAULT_COMPOSITE_CALCULATION_TYPE,
'0' AS  MAP_ALL_DEPT_FLAG,
'3' AS  COMPST_MIN_DLRS_TO_RUN,
'Cumulative' AS  ACCT_DATA_TYP,
'Period' AS  DECIMAL_SEPARATOR_DEFAULT,
'0' AS  FORM_READONLY_FLAG,
'25' AS  CMPST_RUN_MTHS,
'3' AS  CMPST_RUN_YRS,
'1' AS  DISPLAY_LCL_ACCT_NBR,
',' AS  csv_delimiter,
'0' AS  SHOW_PLANS,
NULL AS  INPUT_FORM_TEMPLATE_NAME,
'4' AS  COMPST_RUN_REVIEWSTATUS_DEFAULT,
'0' AS  AllowDuplicateAccountData,
'0' AS  FILE_IMPORT_SCHEMA,
'1' AS  ALLOW_DELETE_COMPOSITE,
'0' AS  Disable_Multiple_StarMap,
'0' AS  AllowMasterAccountMap,
'1' AS  ShowCompositesToVendors,
'0' AS  AllowCompositeClosedSnapShot,
'1' AS  SubmissionFrequencyID,
'0' AS  AutoRecalculate,
'0' AS  ShowDealerCodeAlternate,
'0' AS  ShowSubDealerIndicator,
'0' AS  ShowDealerType,
'0' AS  OnlineBusinessPlan,
'1' AS  ShowInactiveItemsAdvancedCriteria,
'SUBMISSION' AS  HonorRollType,
'0' AS  ShowInactiveDealersDataSubmission,
'0' AS  AllowFinancialDataSuppression,
'1' AS  AllowDataSubmissionComments,
'1' AS  RejectMatchingSubmissions,
'0' AS  HonorRollAccuracyPoint,
'0' AS  HonorRollNonSubmissionPoint,
'0' AS  DisableInputFormAccountHighlight,
'0' AS  DisableInputFormAuditComments,
'0' AS  EnableSubDealers,
'1' AS  ShowCompositesInDealerProfile,
'0' AS  EnableCarryForwardAuditRemark,
'0' AS  EnableInputFormInlineEvents,
'0' AS  PreventOverMap,
'0' AS  DefaultAdhocReportVariablesTab,
'' AS  WorkingCapitalFormula,
'' AS  InvestmentGuideFormula,
'0' AS  DualDealerAppointmentMonthCutoff,
'0' AS  WorkingCapitalSubmissionsStartMonth,
'0' AS  WorkingCapitalSubmissionsEndMonth,
'0' AS  WorkingCapitalSuccessfulSubmissionsMinimum,
'0' AS  WorkingCapitalStartMonth,
'0' AS  WorkingCapitalEndMonth,
'0' AS  WorkingCapitalActivationMonthCutoff,
'0' AS  AdhocShowAllUserReports,
'0' AS  AuditResultsLinkStatus,
'0' AS  ShowHistoricCompositesEntitySearch,
'0' AS  WorkSheetFullScreenMode,
'0' AS  SpecialCarryForward,
NULL AS  DmsFileTransferFormat,
NULL AS  SiteLogo,
'0' AS  SubmissionPointsThreshold,
'0' AS  EnableCompositeAverageByDepartment,
'1' AS  ViewPreviousSubmission,
'0' AS  HideAuditResultZeroValues,
'1' AS  KeepReleasedFileImportData,
'0' AS  ArlAccountNumberParse,
NULL AS  ConvertForecastRoles,
'0' AS  ArlEnableUndo,
'0' AS  UnhideAuditResultSummaryAccountZeroValues,
'0' AS  AlwaysShowAuditResultSummaryAccountCarryForward,
'0' AS  CompositeAverageSummaryAccounts,
'0' AS  OnlineBusinessForecast,
'0' AS  IsFutureDateAllowForDataSubmission,
'0' AS  DynamicPortfolio,
'0' AS  EnableAllocationAccounts,
'0' AS  EnableTermsAndConditions,
'0' AS  EnableSimultaneousAudit

INSERT 语句是在 Excel 电子表格中配置内容后生成的。

【问题讨论】:

  • 实际的插入语句可能非常相关,完整的表定义也可能如此。例如,如果您的列顺序意外地与值顺序不完全匹配,您可能会尝试将 NULL 插入到另一列而不是您想要的。
  • 您能出示insert 声明吗?以及生成 SQL? 的代码
  • 那张桌子上有触发器吗?
  • 您是否在插入时明确提及列名,例如 INSERT INTO (Column_Name) VALUES (1)... 因为它是位列 insert int 10
  • 表上没有触发器。我将更新以包括插入语句以及表定义。在 Excel 中插入语句是 81 行数据的串联。

标签: sql sql-server


【解决方案1】:

我的猜测是 Excel 电子表格中列的顺序与 TableA 的列顺序不匹配。您的列“IsFutureDateAllowForDataSubmission”在 TableA 表定义中位于第 63 位,但在电子表格中位于第 77 位。

如果您在电子表格中查找位置 63,您会发现 DmsFileTransferFormat,在您的情况下其值为 NULL。我猜这是你的错误的根源。

要么排列电子表格中列的顺序以匹配 TableA 中列的顺序,要么像这样在 INSERT 语句中明确指定列:

INSERT INTO TableA (BUSNS_UNIT_ID, BUSNS_UNIT_NM, ALLOW_RPG_EDIT_FLAG, etc.) SELECT
'14' AS BUSNS_UNIT_ID,
'Global' AS  BUSNS_UNIT_NM,
'0' AS  ALLOW_RPG_EDIT_FLAG,
etc

【讨论】:

  • 这个很高兴知道,其实我有点尴尬我没有想到这一点。无论出于何种原因,我认为将列别名为其适当的列名会消除歧义,但显然情况并非如此。感谢您的帮助!
  • 我也很辛苦地学会了这一点,很高兴能帮上忙。 :-)
【解决方案2】:

插入语句中的列与表中的列不同步。您在insert 语句的select 部分中命名列这一事实不会导致插入对齐(因为缺少更好的词)表定义的列。

当执行像您在此处执行的插入操作时,选择语句中的列必须按照您要插入的表的正确顺序排列。检查您的表定义并与选择进行比较,我们在表中的第 63 行:

| IsFutureDateAllowForDataSubmission              |

在选择时我们有:

NULL AS  DmsFileTransferFormat

所以您正在尝试将null 插入列中。要解决此问题,您可以重新排列 select 语句中的列,使其与表定义中的顺序相同,或者您可以在 select 之前添加列顺序:

insert into TableA  (BUSNS_UNIT_ID, ...)
    select ...

然后确保在括号中列出列名的顺序与它们在随后的select 中出现的顺序相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多