SQL Server 2008: Bulk Datatype Change

前端 未结 4 657
失恋的感觉
失恋的感觉 2021-01-20 11:32

I have an SQL Server 2008 database with many tables. I\'ve been using the now lame datetime datatype and want to use the new and better datetime2.

4条回答
  •  时光说笑
    2021-01-20 11:46

    Run this in Management Studio, copy the result and paste into new Query Window:

    select 'ALTER TABLE ' + OBJECT_NAME(o.object_id) + 
        ' ALTER COLUMN ' + c.name + ' DATETIME2 ' +
        CASE WHEN c.is_nullable = 0 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 c.name = 'Timestamp'
    and t.name = 'datetime'
    order by OBJECT_NAME(o.object_id)
    

提交回复
热议问题