“xxxx”表 - 无法修改表。 不能将值 NULL 插入列 'xxxx'

Posted zev_fung

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了“xxxx”表 - 无法修改表。 不能将值 NULL 插入列 'xxxx'相关的知识,希望对你有一定的参考价值。

问题

向已有表增加字段

执行下面sql,sql执行增加两个字段分别:

  1. articleTitle 正标题 [nvarchar](200)
  2. articleSubTitle 副标题 [nvarchar](200)
USE [tg_thirdparty]
GO

/****** Object:  Table [dbo].[WX_Activity]    Script Date: 10/31/2018 10:54:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO


IF NOT EXISTS ( SELECT  1
                FROM    SYSOBJECTS T1
                        INNER JOIN SYSCOLUMNS T2 ON T1.ID = T2.ID
                WHERE   T1.NAME = WX_Activity
                        AND T2.NAME = articleTitle ) 
    BEGIN
    
        ALTER TABLE [dbo].[WX_Activity] ADD [articleTitle] [nvarchar](200) NOT   NULL 
        --ALTER TABLE [dbo].[WX_Activity] ADD  CONSTRAINT [DF_WX_Activity_articleTitle]  DEFAULT (‘‘) FOR [articleTitle]
        --ALTER TABLE [dbo].[WX_Activity] ALTER COLUMN [articleTitle] [nvarchar](200) NOT NULL

     

        EXEC sys.sp_addextendedproperty @name = NMS_Description,
            @value = N正标题, @level0type = NSCHEMA, @level0name = Ndbo,
            @level1type = NTABLE, @level1name = NWX_Activity,
            @level2type = NCOLUMN, @level2name = NarticleTitle


    END
    
GO
    
IF NOT EXISTS ( SELECT  1
                FROM    SYSOBJECTS T1
                        INNER JOIN SYSCOLUMNS T2 ON T1.ID = T2.ID
                WHERE   T1.NAME = WX_Activity
                        AND T2.NAME = articleSubTitle ) 
    BEGIN
    
        ALTER TABLE [dbo].[WX_Activity] ADD [articleSubTitle] [nvarchar](200) NOT  NULL 
        --ALTER TABLE [dbo].[WX_Activity] ADD  CONSTRAINT [DF_WX_Activity_articleSubTitle]  DEFAULT (‘‘) FOR [articleSubTitle]
        --ALTER TABLE [dbo].[WX_Activity] ALTER COLUMN [articleSubTitle] [nvarchar](200) NOT NULL
     

        EXEC sys.sp_addextendedproperty @name = NMS_Description,
            @value = N副标题, @level0type = NSCHEMA, @level0name = Ndbo,
            @level1type = NTABLE, @level1name = NWX_Activity,
            @level2type = NCOLUMN, @level2name = NarticleSubTitle


    END
    
GO
    

 

提示错误

消息 4901,级别 16,状态 1,第 10ALTER TABLE 只允许添加满足下述条件的列: 列可以包含 Null 值;或者列具有指定的 DEFAULT 定义;或者要添加的列是标识列或时间戳列;或者,如果前几个条件均未满足,则表必须为空以允许添加此列。不能将列"articleTitle"添加到非空表"WX_Activity"中,因为它不满足上述条件。
消息 4901,级别 16,状态 1,第 9ALTER TABLE 只允许添加满足下述条件的列: 列可以包含 Null 值;或者列具有指定的 DEFAULT 定义;或者要添加的列是标识列或时间戳列;或者,如果前几个条件均未满足,则表必须为空以允许添加此列。不能将列"articleSubTitle"添加到非空表"WX_Activity"中,因为它不满足上述条件。

 

 

解决方法:

表中已经有数据了,不设置默认值,有要求字段不能为NULL,矛盾了!!

所以先指定默认值,插入字段时,加上默认值 DEFAULT ‘‘

USE [tg_thirdparty]
GO

/****** Object:  Table [dbo].[WX_Activity]    Script Date: 10/31/2018 10:54:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO


IF NOT EXISTS ( SELECT  1
                FROM    SYSOBJECTS T1
                        INNER JOIN SYSCOLUMNS T2 ON T1.ID = T2.ID
                WHERE   T1.NAME = WX_Activity
                        AND T2.NAME = articleTitle ) 
    BEGIN
    
        ALTER TABLE [dbo].[WX_Activity] ADD [articleTitle] [nvarchar](200) NOT   NULL DEFAULT ‘‘
        --ALTER TABLE [dbo].[WX_Activity] ADD  CONSTRAINT [DF_WX_Activity_articleTitle]  DEFAULT (‘‘) FOR [articleTitle]
        --ALTER TABLE [dbo].[WX_Activity] ALTER COLUMN [articleTitle] [nvarchar](200) NOT NULL

     

        EXEC sys.sp_addextendedproperty @name = NMS_Description,
            @value = N正标题, @level0type = NSCHEMA, @level0name = Ndbo,
            @level1type = NTABLE, @level1name = NWX_Activity,
            @level2type = NCOLUMN, @level2name = NarticleTitle


    END
    
GO
    
IF NOT EXISTS ( SELECT  1
                FROM    SYSOBJECTS T1
                        INNER JOIN SYSCOLUMNS T2 ON T1.ID = T2.ID
                WHERE   T1.NAME = WX_Activity
                        AND T2.NAME = articleSubTitle ) 
    BEGIN
    
        ALTER TABLE [dbo].[WX_Activity] ADD [articleSubTitle] [nvarchar](200) NOT  NULL DEFAULT ‘‘
        --ALTER TABLE [dbo].[WX_Activity] ADD  CONSTRAINT [DF_WX_Activity_articleSubTitle]  DEFAULT (‘‘) FOR [articleSubTitle]
        --ALTER TABLE [dbo].[WX_Activity] ALTER COLUMN [articleSubTitle] [nvarchar](200) NOT NULL
     

        EXEC sys.sp_addextendedproperty @name = NMS_Description,
            @value = N副标题, @level0type = NSCHEMA, @level0name = Ndbo,
            @level1type = NTABLE, @level1name = NWX_Activity,
            @level2type = NCOLUMN, @level2name = NarticleSubTitle


    END
    
GO
    

 

以上是关于“xxxx”表 - 无法修改表。 不能将值 NULL 插入列 'xxxx'的主要内容,如果未能解决你的问题,请参考以下文章

无法将值插入 MySQL 表

无法将值插入表中,外键约束不断失败

无法将值存储到 oracle 中的嵌套表中

无法将值 NULL 插入列“id”、表“XXX”;列不允许空值。插入失败

ORA-01653: 表 xxxx 无法通过 8192 (在表空间 USERS 中) 扩展

无法将值 NULL 插入列“TaskID”、表“TEAM.dbo.Task”;列不允许空语句已终止[重复]