“xxxx”表 - 无法修改表。 不能将值 NULL 插入列 'xxxx'
Posted zev_fung
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了“xxxx”表 - 无法修改表。 不能将值 NULL 插入列 'xxxx'相关的知识,希望对你有一定的参考价值。
问题
向已有表增加字段
执行下面sql,sql执行增加两个字段分别:
- articleTitle 正标题 [nvarchar](200)
- 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 = N‘MS_Description‘, @value = N‘正标题‘, @level0type = N‘SCHEMA‘, @level0name = N‘dbo‘, @level1type = N‘TABLE‘, @level1name = N‘WX_Activity‘, @level2type = N‘COLUMN‘, @level2name = N‘articleTitle‘ 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 = N‘MS_Description‘, @value = N‘副标题‘, @level0type = N‘SCHEMA‘, @level0name = N‘dbo‘, @level1type = N‘TABLE‘, @level1name = N‘WX_Activity‘, @level2type = N‘COLUMN‘, @level2name = N‘articleSubTitle‘ END GO
提示错误
消息 4901,级别 16,状态 1,第 10 行 ALTER TABLE 只允许添加满足下述条件的列: 列可以包含 Null 值;或者列具有指定的 DEFAULT 定义;或者要添加的列是标识列或时间戳列;或者,如果前几个条件均未满足,则表必须为空以允许添加此列。不能将列"articleTitle"添加到非空表"WX_Activity"中,因为它不满足上述条件。 消息 4901,级别 16,状态 1,第 9 行 ALTER 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 = N‘MS_Description‘, @value = N‘正标题‘, @level0type = N‘SCHEMA‘, @level0name = N‘dbo‘, @level1type = N‘TABLE‘, @level1name = N‘WX_Activity‘, @level2type = N‘COLUMN‘, @level2name = N‘articleTitle‘ 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 = N‘MS_Description‘, @value = N‘副标题‘, @level0type = N‘SCHEMA‘, @level0name = N‘dbo‘, @level1type = N‘TABLE‘, @level1name = N‘WX_Activity‘, @level2type = N‘COLUMN‘, @level2name = N‘articleSubTitle‘ END GO
以上是关于“xxxx”表 - 无法修改表。 不能将值 NULL 插入列 'xxxx'的主要内容,如果未能解决你的问题,请参考以下文章
无法将值 NULL 插入列“id”、表“XXX”;列不允许空值。插入失败