无法增加列大小

时间:2012-08-06 12:00:05

标签: sql-server sql-server-2000

我需要增加表的列大小,我使用下面的查询来增加大小,但我得到的误差

Alter Table Tabl1 Alter Column Col1 VarChar(6) Not NULL
  

Msg 5074,Level 16,State 1,Line 1
  对象'Tabl1'依赖于列'Col1'   Msg 5074,Level 16,State 1,Line 1
  统计信息'_WA_Sys_Col1_5070F446'取决于列'Col1'   Msg 4922,Level 16,State 9,Line 1
  ALTER TABLE ALTER COLUMN Col1失败,因为一个   或更多对象访问此列。

因为同一个表作为对列的依赖

需要帮助

2 个答案:

答案 0 :(得分:4)

SQL Server会自动将统计信息随时间添加到表中,以便在分析查询和构建查询计划时使用。您必须删除统计信息才能更改列。例如:

drop statistics [dbo].[Tabl1].[_WA_Sys_Col1_5070F446]

但是,在删除之前,应使用SSMS查看_WA_Sys_Col1_5070F446统计信息中的列,以便可以重新创建它。像这样:

create statistics [_WA_Sys_Col1_5070F446] on [dbo].[Tabl1]([Col1])

但可能会有更多列...,因此请务必在删除之前找出需要包含的内容。

您可以运行此SQL来查找大多数依赖项,它不会报告统计信息依赖项,但它会捕获大多数其他依赖项:

SELECT OBJECT_NAME(d.object_id) AS SP_Or_Function, OBJECT_NAME(d.referenced_major_id) AS TableReferenced
FROM   sys.sql_dependencies AS d INNER JOIN
       sys.all_sql_modules AS m ON m.object_id = d.object_id
where  OBJECT_ID(N'Tabl1') = d.referenced_major_id
GROUP BY OBJECT_NAME(d.object_id), OBJECT_NAME(d.referenced_major_id)
ORDER BY SP_Or_Function, TableReferenced

您可以使用此查询找到给定表使用的所有统计信息:

SELECT DISTINCT
OBJECT_NAME(s.[object_id]) AS TableName,
c.name AS ColumnName,
s.name AS StatName,
s.auto_created,
s.user_created,
s.no_recompute,
s.[object_id],
s.stats_id,
sc.stats_column_id,
sc.column_id,
STATS_DATE(s.[object_id], s.stats_id) AS LastUpdated
FROM sys.stats s JOIN sys.stats_columns sc ON sc.[object_id] = s.[object_id] AND sc.stats_id = s.stats_id
JOIN sys.columns c ON c.[object_id] = sc.[object_id] AND c.column_id = sc.column_id
JOIN sys.partitions par ON par.[object_id] = s.[object_id]
JOIN sys.objects obj ON par.[object_id] = obj.[object_id]
WHERE OBJECTPROPERTY(s.OBJECT_ID,'IsUserTable') = 1
AND (s.auto_created = 1 OR s.user_created = 1)
AND object_id(N'Tabl1') = s.[object_id]

感谢SQLAuthority提供的最后两个SQL查询:

SQL SERVER – Get the List of Object Dependencies – sp_depends and information_schema.routines and sys.dm_sql_referencing_entities (Gabriel's post)

SQL SERVER – Find Details for Statistics of Whole Database – DMV – T-SQL Script

答案 1 :(得分:0)

以下是SQL Server 2000帮助的引用:

  
    

ALTER COLUMN

         

更改的列不能是:

         

.....

         
        
  • 在索引中使用,除非该列是varchar,nvarchar或varbinary数据类型,否则数据类型不会更改,并且新大小为     等于或大于旧尺寸。

  •     
  • 用于CREATE STATISTICS语句生成的统计信息。首先使用DROP STATISTICS语句删除统计信息。     查询优化器自动生成的统计信息     由ALTER COLUMN自动删除。 .....

  •