无法插入值null错误但值isn实际上不为null

时间:2014-08-18 21:45:51

标签: sql sql-server

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

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'。两者都会导致相同的错误。

到目前为止,除了尝试在设置为标识的列中添加值的人之外,我还没有找到很多在线,但这种情况与此无关,因为我尝试添加值的列是a简单的0/1标志,不是身份。

如果需要,我很乐意提供更多信息。实际的insert语句很长(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

在Excel电子表格中配置事物后生成INSERT语句。

2 个答案:

答案 0 :(得分:3)

我的猜测是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

答案 1 :(得分:1)

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

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

| IsFutureDateAllowForDataSubmission              |

在选择中我们有:

NULL AS  DmsFileTransferFormat

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

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

然后确保列出括号中的列名称,其顺序与后续select中显示的顺序相同。