日期:2014-05-17  浏览次数:20759 次

MSSQL2008 修改列长度时不成功,请指点
这里是创建表的脚本;
CREATE TABLE [dbo].[XXXXXX]
  (
  [Objid] [int] IDENTITY(1, 1)
  NOT NULL ,
  [Parentid] [int] NOT NULL ,
  [Objname] [varchar](20) NULL ,
  [Objjc] [varchar](50) NULL ,
  [AFIRSTQ0001] [datetime] NULL ,
  [ESECONDQ0722] [datetime] NULL ,
  [ESECONDQ0914] [datetime] NULL ,
  [ESECONDI0734] [int] NULL ,
  [AFIRSTC0530] [varchar](1000) NULL ,
  [AFIRSTC0003] [varchar](50) NULL ,
  [AFIRSTC0557] [varchar](100) NULL ,
  [AFIRSTQ2045] [datetime] NULL ,
  [memo_PRE] AS ( CASE WHEN ( CHARINDEX('$$$',CONVERT(VARCHAR(1000), [memo])) > 0 ) THEN 1 ELSE 0 END ) ,
  [AFIRSTQ0023] [datetime] NULL ,
  [AFIRSTC0537] [varchar](100) NULL ,
  [AFIRSTC2515] [varchar](255) NULL ,
  [ESECONDI0003] [int] NULL ,
  [WFPColor1] AS ( [dbo].[f_get](( -30 ), [AFIRSTQ2045], [OBJNAME],GETDATE()) ) ,
  [ItemCount] AS ( [dbo].[f_get_I]([objexplain]) ) ,
  CONSTRAINT [IX_WFPUSER_A0113] UNIQUE NONCLUSTERED ( [ESECONDQ0914] ASC )
  WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
  IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90 ) ON [PRIMARY]
  )
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

想着对 [Objname] 列进行修改;
执行的语句是 ALTER TABLE XXXXXX ALTER COLUMN Objname varchar(50)
提示内容是:

消息 5074,级别 16,状态 1,第 1 行
列'WFPColor1' 依赖于 列'Objname'。
消息 4922,级别 16,状态 9,第 1 行
由于一个或多个对象访问此列,ALTER TABLE ALTER COLUMN Objname 失败。




------解决方案--------------------
SQL code
CREATE TABLE [dbo].[XXXXXX]
  (
  [Objid] [int] IDENTITY(1, 1)
  NOT NULL ,
  [Parentid] [int] NOT NULL ,
  [Objname] [varchar](20) NULL ,
  [Objjc] [varchar](50) NULL ,
  [AFIRSTQ0001] [datetime] NULL ,
  [ESECONDQ0722] [datetime] NULL ,
  [ESECONDQ0914] [datetime] NULL ,
  [ESECONDI0734] [int] NULL ,
  [AFIRSTC0530] [varchar](1000) NULL ,
  [AFIRSTC0003] [varchar](50) NULL ,
  [AFIRSTC0557] [varchar](100) NULL ,
  [AFIRSTQ2045] [datetime] NULL ,
  [memo_PRE] AS ( CASE WHEN ( CHARINDEX('$$$',CONVERT(VARCHAR(1000), [memo])) > 0 ) THEN 1 ELSE 0 END ) ,
  [AFIRSTQ0023] [datetime] NULL ,
  [AFIRSTC0537] [varchar](100) NULL ,
  [AFIRSTC2515] [varchar](255) NULL ,
  [ESECONDI0003] [int] NULL ,
  [WFPColor1] AS ( [dbo].[f_get](( -30 ), [AFIRSTQ2045], [OBJNAME],GETDATE()) ) ,  --就这里,需要先拆了,再加上.
  [ItemCount] AS ( [dbo].[f_get_I]([objexplain]) ) ,
  CONSTRAINT [IX_WFPUSER_A0113] UNIQUE NONCLUSTERED ( [ESECONDQ0914] ASC )
  WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
  IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90 ) ON [PRIMARY]
  )
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

------解决方案--------------------
因为找个列引用了OBJNAME

[WFPColor1] AS ( [dbo].[f_get](( -30 ), [AFIRSTQ2045], [OBJNAME],GETDATE()) ) ,
需要把这一列先删除
然后在修改,然后在重建找个列
------解决方案--------------------
因含计算列,有依赖关系,
先备份数据,然后删除-重建表试试..
------解决方案--------------------
步骤:
SQL code

步骤:
ALTER TABLE XXXXXX DROP COLUMN WFPColor1
ALTER TABLE XXXXXX ALTER COLUMN Objname varchar(50)
ALTER TABLE XXXXXX ALTER COLUMN Objname varchar(50)
ALTER TABLE XXXXXX  ADD WFPColor1 AS ( [dbo].[f_get](( -30 ), [AFIRSTQ2045], [OBJNAME],GETDATE()) )

------解决方案--------------------
SQL code
ALTER TABLE XXXXXX DROP COLUMN WFPColor1
ALTER TABLE XXXXXX ALTER COLUMN Objname varchar(50)
ALTER TABLE XXXXXX ALTER COLUMN Objname va