schmir1
asked on
Script to change nvarchar to varchar for all my tables
I need some T-SQL script to change all "nvarchar" columns to "varchar" for all my tables in my database. How would I do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I assume you are positive that your columns have no unicode data?
ASKER
Thanks. I'm trying to run your script in the Server Management Studio but get the following errors:
Msg 5074, Level 16, State 1, Line 1
The object 'UK_principal_name' is dependent on column 'name'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN name failed because one or more objects access this column.
Msg 5074, Level 16, State 1, Line 1
The object 'UK_principal_name' is dependent on column 'name'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN name failed because one or more objects access this column.
You are going to have to do this manually.
Or do a major overhaul on the procedure so that it drops and re-creates the dependent objects.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cesnasa: I tried it with a test table but it didn't change anything even though this test table didn't have any constraints? I get the following in the results window.
I'm starting to be convince that I will have to do it manually. So far my SQL Server experiences are that it is very hard to do anything)-:
I'm starting to be convince that I will have to do it manually. So far my SQL Server experiences are that it is very hard to do anything)-:
ALTER COLUMN Test Format VARCHAR(20) ; ;ALTER TABLE test ALTER COLUMN col1 VARCHAR(50) ; ;ALTER TABLE test ALTER COLUMN col2 VARCHAR(50) ;
Another problem with csenasa's post is that it doesn't take into account the current NULL setting of a column. You could potentially be changing all of your columns to the default of NULL.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It took about 3 hours but I did it manually. Thanks for all your suggestions.