1

我有测试数据库,我试图用索引调整列的大小。表有几行。不多说,上代码

Create Table Country (ID INT PRIMARY KEY IDENTITY, NAME VARCHAR(48),ENEMY VARCHAR(32),AMOUNT INT);

CREATE NONCLUSTERED INDEX IDX_COUNTRY_NAME ON Country(NAME);

我添加了一些行。稍后使用 sql 语句来调整列的大小,如下所示:

ALTER INDEX IDX_COUNTRY_NAME ON Country disable;

alter table COUNTRY alter column NAME VARCHAR(40);

ALTER INDEX IDX_COUNTRY_NAME ON Country rebuild;`

这就是我尝试更新专栏后得到的

java.sql.BatchUpdateException: The index 'IDX_COUNTRY_NAME' is dependent on column 'NAME'.
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
    at com.veraxsystems.commons.updater.tasks.ResizeColumnTask.execute(ResizeColumnTask.java:52)
    at com.veraxsystems.commons.updater.tasks.AbstractTask.process(AbstractTask.java:79)
    at com.veraxsystems.commons.Updater.executeUpdate(Updater.java:169)
    at com.veraxsystems.commons.Updater.afterPropertiesSet(Updater.java:201)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我想避免删除索引,因为我不知道将来是否存在随机索引的脚本,并且我在重新加载它们时会遇到问题。我使用 sql server 2008 我尝试了很多方法来完成这个。唯一的希望是使用 drop,但正如我之前所说,我宁愿不使用它。

编辑

这些只是示例 sql,我知道我更新了哪个表和列。为了找到它,我使用 sys.indexes、sys.index_columns 并通过 object_id 加入它们。这很好用,因为我打印了所有结果并且符合预期。

4

1 回答 1

1

从 ALTER TABLE 的手册中,ALTER COLUMN 部分:

修改后的列不能是以下任何一项:

[…]

  • 在索引中使用,除非列是 varchar、nvarchar 或 varbinary 数据类型,数据类型不会改变,新大小等于或大于旧大小,并且索引不是 PRIMARY KEY 的结果约束。

[…]

(斜体和重点是我的。)

您正在尝试减小索引列的大小(从(varchar(48)varchar(40)),这是“违反规则”的。似乎除了删除/重新创建索引之外没有其他选择。

于 2012-09-05T11:15:36.350 回答