Solved

Script to change nvarchar to varchar for all my tables

Posted on 2009-05-14
12
843 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 100 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 100 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 200 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 100 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 200 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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