SQL Server 2008: Bulk Datatype Change

前端 未结 4 661
失恋的感觉
失恋的感觉 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 12:03

    This would be a bit of a brute-force method, but you could always look up all columns of datatype datetime using the sys.columns view, grab the table name and column name, iterate over that list with a cursor, and for each entry generate an ALTER TABLE statement like so:

    ALTER TABLE @tablename ALTER COLUMN @columnname datetime2
    

    Then run said statement with EXEC. Obviously, you'd need to have permissions both to query sys.columns and to ALTER all of those tables...

    Apologies there isn't more code in this answer - don't have a copy of SSMS on this machine, and can't remember the syntax for all of that from memory. :)

提交回复
热议问题