码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-10-31 12:41:08      阅读:1259      评论:0      收藏:0      [点我收藏+]

标签:alt   ted   问题   null   消息   varchar   party   提示   padding   

问题

向已有表增加字段

执行下面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'

标签:alt   ted   问题   null   消息   varchar   party   提示   padding   

原文地址:https://www.cnblogs.com/Zev_Fung/p/9882077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!