Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert datatypes in a table to nvarchar.

Posted on 2014-04-01
3
Medium Priority
?
450 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 2000 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 70

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 70

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

876 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