Varchar(255) to Varchar(MAX)

前端 未结 3 808
[愿得一人]
[愿得一人] 2021-02-04 23:27

Is it possible to change a column type in a SQL Server 2008 database from varchar(255) to varchar(MAX) without having to drop the table and recreate?

3条回答
  •  故里飘歌
    2021-02-05 00:31

    Be aware
    with Something like

    ALTER TABLE [table] ALTER COLUMN [column] VARCHAR(MAX)
    

    https://dba.stackexchange.com/questions/15007/change-length-of-varchar-on-live-prod-table

    Martin Smith's answare:

    If you are increasing it to varchar(100 - 8000) (i.e. anything other than varchar(max)) and you are doing this through TSQL rather than the SSMS GUI

    ALTER TABLE YourTable ALTER COLUMN YourCol varchar(200) [NOT] NULL
    and not altering column nullability from NULL to NOT NULL (which would lock the table while all rows are validated and potentially written to or from NOT NULL to NULL in some circumstances then this is a quick metadata only change. It might need to wait for a SCH-M lock on the table but once it acquires that the change will be pretty much instant. One caveat to be aware of is that during the wait for a SCH-M lock other queries will be blocked rather than jump the queue ahead of it so you might want to consider adding a SET LOCK_TIMEOUT first. Also make sure in the ALTER TABLE statement you explicitly specify NOT NULL if that is the original column state as otherwise the column will be changed to allow NULL.

提交回复
热议问题