Convert Char to VarChar

  • I have inherited a database system where all text fields are defined as char. This has lead to the database .mdf file growing to an enormous size, to remedy this problem I want to convert all char fields that are greater than 1 in length to a varchar. I know that I can do this within enterprise manager but I want to be able to use T-SQL or a stored procedure to generate scripts as I have lots of tables. The difficulty that I have is that I will need to drop and then re-create the constraints that exist on the fields. Does anyone know of a stored procedure / codes that could assist in this?

    many thanks

    Phil

  • Attached is a script I used to create a script to convert char(1) columns to bit. I drop any constraints that include the column, check constraints on the column, indexes, foreign keys, and because I was changing the type I update the data, change the data type and add a default. You should be able to modify it to fit your needs.

    I just noticed this is a 7,2000 forum and my script is for 2005 and uses the new sys schema views for parts and you'd need to find the corresponding sys tables in 7. 2000. Most of it uses the information_schema views which you should have as well.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply