Sql server 在SQL Server中多次运行alter table alter column语句

Sql server 在SQL Server中多次运行alter table alter column语句,sql-server,Sql Server,在SQL Server中多次运行alter table alter column语句是否有任何负面影响 假设我更改列的数据类型和可空性,如下所示: --create table create table Table1 ( Column1 varchar(50) not null ) go --insert some records insert into Table1 values('a') insert into Table1 values('b') go --alter on

在SQL Server中多次运行alter table alter column语句是否有任何负面影响

假设我更改列的数据类型和可空性,如下所示:

--create table
create table Table1 
(
    Column1 varchar(50) not null
) 
go

--insert some records
insert into Table1 values('a')
insert into Table1 values('b')
go

--alter once
alter table Table1 
    alter column Column1 nvarchar(250) not null
go

--alter twice
alter table Table1 
    alter column Column1 nvarchar(250) not null
go
上面的一组sql都可以工作,我已经测试过了。我还可以测试alter语句中的属性。问题是,在更改之前检查列是否已经可以为空有什么好处

在第一次更改之后,SQL Server是否发现表已经被更改,因此第二次更改实际上什么也不做

不同版本的SQL Server在如何处理这一问题上是否存在差异

谢谢,
Ilias

这是一个仅元数据操作

它不必读取或写入属于
表1
的任何数据页。但这并不是一个完全没有行动的计划

它仍将启动事务,获取表上的模式修改锁,并更新
sys.sysschobjs
中该表行中的
modified
列(通过
sys.objects
中的
modified\u date
列向我们公开)


此外,由于该表已被修改,任何引用该表的执行计划都需要在下次使用时重新编译。

您尝试过吗?会发生什么?另外,您可以检查您的列是否存在,或者检查其他属性并有条件地执行alter语句。我想不出有什么区别。您的语句在它们自己的批处理中是自包含的(即
GO
语句)。例如,尝试添加列并在同一批中引用它。它会出错。@Jeremy我已经试过了,并且按照预期工作,我编辑了这个问题,希望能把它弄清楚。谢谢