zoukankan      html  css  js  c++  java
  • “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 = 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,第 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 = 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
        
    学习,以记之。如有错漏,欢迎指正

    作者:冯子武
    出处:http://www.cnblogs.com/Zev_Fung/
    本文版权归作者和博客园所有,欢迎转载,转载请标明出处。
    如果博文对您有所收获,请点击下方的 [推荐],谢谢

  • 相关阅读:
    axis2 WebService的发布与调用
    sql语句having子句用法,很多时候你曾忘掉
    linux下tomcat开机自启动
    框架使用的技术主要是SpringMVC 在此基础上进行扩展
    SpringMVC整合Mongodb开发 架构搭建
    解决Linux下3T硬盘分区只有2T(2199G)可用
    ubuntu cp(copy) command
    Linux如何根据UUID自动挂载磁盘分区
    python exec和eval
    在OpenERP报表中使用selection 类型字段
  • 原文地址:https://www.cnblogs.com/Zev_Fung/p/9882077.html
Copyright © 2011-2022 走看看