Answers:
-- replace NVARCHAR(42) with the actual type of your column
ALTER TABLE your_table
ALTER COLUMN your_column NVARCHAR(42) NULL
ALTER TABLE your_table ALTER COLUMN your_column NVARCHAR(42)
,如果没有另外明确指定,则默认情况下还是允许空值。
ALTER TABLE .. ALTER COLUMN ..
命令
ALTER TABLE ALTER COLUMN failed because one or more objects access this column.
是的,您可以ALTER TABLE
按如下方式使用:
ALTER TABLE [table name] ALTER COLUMN [column name] [data type] NULL
引用ALTER TABLE
文档:
NULL
可以指定ALTER COLUMN
来强制NOT NULL
列允许空值,但PRIMARY KEY约束中的列除外。
ALTER TABLE是正确的:
ALTER TABLE MyCustomers ALTER COLUMN CompanyName VARCHAR(20) NULL
ALTER TABLE public.contract_termination_requests
ALTER COLUMN management_company_id DROP NOT NULL;
我写了这个,所以我可以一次将所有表和列编辑为空:
select
case
when sc.max_length = '-1' and st.name in ('char','decimal','nvarchar','varchar')
then
'alter table [' + so.name + '] alter column [' + sc.name + '] ' + st.name + '(MAX) NULL'
when st.name in ('char','decimal','nvarchar','varchar')
then
'alter table [' + so.name + '] alter column [' + sc.name + '] ' + st.name + '(' + cast(sc.max_length as varchar(4)) + ') NULL'
else
'alter table [' + so.name + '] alter column [' + sc.name + '] ' + st.name + ' NULL'
end as query
from sys.columns sc
inner join sys.types st on st.system_type_id = sc.system_type_id
inner join sys.objects so on so.object_id = sc.object_id
where so.type = 'U'
and st.name <> 'timestamp'
order by st.name
这是执行此操作的方法:-
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_CATALOG = 'DBName' AND
TABLE_SCHEMA = 'SchemaName' AND
TABLE_NAME = 'TableName' AND
COLUMN_NAME = 'ColumnName')
BEGIN
ALTER TABLE DBName.SchemaName.TableName ALTER COLUMN ColumnName [data type] NULL
END
如果您没有任何架构,请删除架构行,因为您无需提供默认架构。
所以最简单的方法是
alter table table_name change column_name column_name int(11) NULL;