Solved

convert datatypes in a table to nvarchar.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL LINE CONTINUATION ISSUE 12 32
Sql server insert 13 27
Query Help - MSSQL - Averages 5 25
SQL - Copy data from one database to another 6 19
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

785 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