Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script to change nvarchar to varchar for all my tables

Posted on 2009-05-14
12
Medium Priority
?
857 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:schmir1
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 400 total points
ID: 24389374
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24389422
I assume you are positive that your columns have no unicode data?
0
 

Author Comment

by:schmir1
ID: 24389683
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24391650
You are going to have to do this manually.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24391718
Or do a major overhaul on the procedure so that it drops and re-creates the dependent objects.
0
 
LVL 3

Assisted Solution

by:csenasa
csenasa earned 400 total points
ID: 24392641
Hi,

Try this. It will Change the Nvarchar Columns to Varchar of all tables at one shot!!

NOTE : IF there are Any Key Constraints present on the Column then those columns will fail.
DECLARE @VarQuery VARCHAR(8000)
SELECT  @VarQuery = ISNULL(@VarQuery + ';' , '' ) +  'ALTER TABLE ' + TAble_Name + ' ALTER COLUMN ' 
+   Column_Name + ' VARCHAR(' +  CAST(Character_MAximum_length as VARCHAR(12)) + ') ; '--, * 
FROM Information_schema.columns where Data_type = 'nvarchar'
 
SELECT (@VarQuery)

Open in new window

0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 800 total points
ID: 24396541
cesnasa: your script suffers from the same thing that the first posted answer does in that it does not account for dependant objects.  it is already evident that there ARE dependent objects based upon the supplied error.
0
 

Author Comment

by:schmir1
ID: 24397280
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)-:
ALTER COLUMN Test Format VARCHAR(20) ; ;ALTER TABLE test ALTER COLUMN col1 VARCHAR(50) ; ;ALTER TABLE test ALTER COLUMN col2 VARCHAR(50) ;

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24397676
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.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 400 total points
ID: 24398155
>>I'm starting to be convince that I will have to do it manually<<
I agree.
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 800 total points
ID: 24398438
"I'm starting to be convince that I will have to do it manually"

Manually or through SSMS where it handles a lot of this stuff for you.

JUST KNOW!!!!! that indexes are bound to those columns as well.  And if you are changing the data type of a column, any and all indexes that depend on that table will be rebuilt.  If you have any large tables, that could be time consuming.  

Perhaps you would be best served attempting this in a test environment first.
0
 

Author Comment

by:schmir1
ID: 24484319
It took about 3 hours but I did it manually.  Thanks for all your suggestions.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question