Solved

Script to change nvarchar to varchar for all my tables

Posted on 2009-05-14
12
834 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:
RQuadling 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now