Solved

convert datatypes in a table to nvarchar.

Posted on 2014-04-01
3
427 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39969283
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:Scott Pletcher
ID: 39969331
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:Scott Pletcher
ID: 39969332
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

860 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