Solved

Script to change nvarchar to varchar for all my tables

Posted on 2009-05-14
12
851 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pivot Query Problem 9 53
Inserting a column in a table that creates an ID and row number 4 76
Query group by data in SQL Server - cursor? 3 87
denied execute as 13 57
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…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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