Modify table: How to change 'Allow Nulls' attribute from not null to allow null

后端 未结 8 847
星月不相逢
星月不相逢 2020-12-12 13:48

How to change one attribute in a table using T-SQL to allow nulls (not null --> null)? Alter table maybe?

8条回答
  •  -上瘾入骨i
    2020-12-12 14:42

    I wrote this so I could edit all tables and columns to null at once:

    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
    

提交回复
热议问题