Solved

convert datatypes in a table to nvarchar.

Posted on 2014-04-01
3
420 Views
Last Modified: 2014-04-01
I have a table with 184 fields that are all defines as varchar. Is there a way to convert all the fields to nvarchar?
Thanks in Advance.
0
Comment
Question by:tesla764
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
you can run this query, and use the outcome of the column "sql" to run it:
select table_schema, table_name,column_name,  character_octet_length
, 'ALTER TABLE [' + table_schema + '].[' + table_name + '] alter column [' + column_name + '] nvarchar(' + cast(character_octet_length as varchar(10)) + ')' sql
from INFORMATION_SCHEMA.columns
where data_type = 'varchar'
and table_name = 'your_table_name'

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
I'd do them all at once, or this could for an extremely long time, given that (almost) every update could potentially force another page split.

Since SQL has no native way to do this all at once, you'll need to create a separate conversion table.  We'll need to lock people out of the table until the conversion completes.

First, ccript out your existing table, then change the table name and all "varchar" to "nvarchar", then run the new CREATE TABLE:

-- original table name = 'table_name'
CREATE TABLE dbo.table_name_NVARCHAR (
    col1 nvarchar(...) ...,
    col2 nvarchar(...) ...,
    )

Then, something like this:

BEGIN TRY
BEGIN TRANSACTION
--SET IDENTITY_INSERT table_name ON
INSERT INTO dbo.table_name_NVARCHAR --( ...column_list... )
SELECT * --...column_list...
FROM dbo.table_name WITH (TABLOCKX)
--SET IDENTITY_INSERT table_name OFF
DROP TABLE dbo.table_name
EXEC sp_rename 'table_name_NVARCHAR', 'table_name'
COMMIT
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0
    ROLLBACK TRANSACTION
END CATCH
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Sorry, if I'd known you were in that big a hurry, I wouldn't have wasted my time trying to offer an alternative.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

743 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

7 Experts available now in Live!

Get 1:1 Help Now