可以改變由線錶行,也可以編寫動態腳本打印的聲明,執行它
CREATE TABLE EXAMPLE
([Colum1] int,
[Colum2] int,
[Colum3] int,
[Colum4] int)
select 'ALTER TABLE ' + OBJECT_NAME(o.object_id) +
' ALTER COLUMN ' + c.name + ' int ' +
CASE WHEN c.is_nullable = 1 THEN 'NOT NULL' ELSE 'NULL' END
from sys.objects o
inner join sys.columns c on o.object_id = c.object_id
inner join sys.types t on c.system_type_id = t.system_type_id
where o.type='U' AND O.name = 'EXAMPLE'
order by OBJECT_NAME(o.object_id)
結果集:
ALTER TABLE EXAMPLE ALTER COLUMN Colum1 int NOT NULL
ALTER TABLE EXAMPLE ALTER COLUMN Colum2 int NOT NULL
ALTER TABLE EXAMPLE ALTER COLUMN Colum3 int NOT NULL
ALTER TABLE EXAMPLE ALTER COLUMN Colum4 int NOT NULL
執行聲明
[請求此連接項](https://connect.microsoft.com/SQLServer/feedback/details/351514/multiple-alter-column-clauses-in-an-alter-table) –