Showing posts with label unicode. Show all posts
Showing posts with label unicode. Show all posts

Thursday, March 29, 2012

Convert a Unicode Database into a non-unicode database

Hi there.

We have an application that can run on a non-unicode or a unicode sql
server
database.

Currently the application is running in a unicode database, as a
non-unicode database is less than half the size, I would prefer to
have a non-unicode database for demo purposes to be on my laptop, etc
etc

Is it possible to change a unicode sql server 2000 database into a
non-unicode database?

And if so, how would I go about doing this?

Any help would be greatly appreciated.

Thanks

RodgerUnicode data uses NCHAR / NVARCHAR / NTEXT datatypes rather than CHAR /
VARCHAR / TEXT. This is defined at the column level rather than the database
level.

To change your database you could script all the tables, replace the unicode
datatypes with their non-unicode equivalents and then run the creation
script to create the new database. This might affect the behaviour of stored
procedures and other code but if the app is designed to cope with this then
presumably it shouldn't be a problem. Correctly converting your unicode data
to a non-unicode collation might be more of a challenge though.

--
David Portas
SQL Server MVP
--|||wsbackup@.hotmail.com (Rodger Dodger) wrote in message news:<c1467e75.0408102040.6037ee2f@.posting.google.com>...
> Hi there.
> We have an application that can run on a non-unicode or a unicode sql
> server
> database.
> Currently the application is running in a unicode database, as a
> non-unicode database is less than half the size, I would prefer to
> have a non-unicode database for demo purposes to be on my laptop, etc
> etc
> Is it possible to change a unicode sql server 2000 database into a
> non-unicode database?
> And if so, how would I go about doing this?
> Any help would be greatly appreciated.
> Thanks
> Rodger

I'm not sure I understand your question - MSSQL always supports
Unicode in all databases. You can change a database's collation, but
that doesn't affect Unicode support, so I guess you may want to know
how to change all your nvarchar columns to varchar? If so, then one
way is to execute the results of a query like this:

SELECT 'ALTER TABLE ' + TABLE_SCHEMA + '.' + TABLE_NAME + ' ALTER
COLUMN ' + COLUMN_NAME + ' VARCHAR(' + CAST(CHARACTER_MAXIMUM_LENGTH
AS VARCHAR(10)) + ')'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'NVARCHAR'

This is a rather heavy-handed approach, however, so make sure you have
a backup before running it. If this doesn't help, or isn't what you're
trying to do, then please clarify what you need to achieve.

Simon

Thursday, March 22, 2012

Conversion from char to Nchar

Hello,

I am trying to convert a single code page MS Server database into a unicode database, using the unicode data types,NCHAR, NVARCHAR, NTEXT. The problem is that in the original database, indexes and constraints have been defined on the tables whose configurations need to be changed. As a result, the ALTER TABLE command fails. Are there any other alternative solutions?
Also, data from the old database needs to be preserved. The objective is to create a unicode database which keeps the old data intact as well as accepts the new data in unicode.
It would be great if you could help!
Thanks,
Sheetal.If you have enough disk space, I strongly recommend:

1) Use SQL Enterprise Manager to script your old database
2) Edit the script to change CHAR to NCHAR
3) Edit the script to change VARCHAR to NVARCHAR
4) Edit the script to change TEXT to NTEXT
5) Create a new database
6) Play the script into the new database using SQL Query Analyzer
7) Copy the data from the old database to the new one

The down side is that this new database can take about 2.5 times as much disk space as your old database, so you have to have quite a bit of space free to make this happen.

There are other ways to do this conversion, but they are a lot more complicated. If you have the disk space, this is a much simpler way to do the conversion.

-PatP|||Hello there,

Thanks very much for your speedy reply!
Excuse me if i sound like a complete beginner, but I am not really experienced with MS Server, as a result, I'm not aware of whether my approach to scripting the database is correct or not. Is it, right click on the existing database -> All new tasks -> Generate SQL Script , and if so,then , General->Show All ;Options->All checkboxes selected??
When the script is ready, i try to execute it on the 'master' DB, after renaming the existing database.
But I get errors and it doesn't execute saying it that it doesn't recognise the user defined data types and roles (while giving their names)
How can I take care of this?
Lastly, after changing the concerned fields, i.e, char to Nchar, varchar to Nvarchar and text to Ntext, which tool is used to transfer the data from the old DB to the new empty one?

Thanks again!
Sheetal.|||That's Ok, everybody has to start somewhere!

First, create a new database. If you prefer working in a GUI environment, you can do this using SQL Enterprise Manager.

Next, open SQL Query Analyzer. Connect to the database, then open the edited script. Click on the "play" button in the toolbar, or just hit Ctrl-E to execute. The script should run in the new database with no error messages.

The simplest way to move the data is probably to use the DTS Wizard. You can get to it by right clicking the Data Transformation Service in SQL Enterprise Manager.

-PatP|||Hello Pat :-)

Many thanks again for your speedy (and warm) reply!
I tried using the SQL Enterprise Manager to create a new database, and then play into it the edited version of the old DB Script, but it gives me really wierd errors everytime, like a certain table or type doesn't exist(though it does exist in the original DB), and there's no way I can find out what's going on with the automated script generation. Any tips?
If not, I wrote a piece of code which works perfectly in converting Char to Nchar :
ALTER TABLE t_nm_reports
DROP CONSTRAINT UQ__t_nm_reports__5D60DB10

ALTER TABLE t_nm_reports
ALTER COLUMN nm_dw_name NCHAR(50) /*the column which needs to be changed*/

ALTER TABLE t_nm_reports
ADD CONSTRAINT UQ__t_nm_reports__5D60DB10
UNIQUE (nm_dw_name);

But, this is a very rough and basic way of solving the problem, done manually for each concerned table. I'm looking for a piece of code which can search for the concerned tables and perform the query, all in one program. Is that possible?
Thank you for your time!

Sheetal.|||I'm going to be really, really detailed about this. Please don't be offended, I'm trying to cover everything, not insult anyone!

1) Launch SQL Enterprise Manager
2) Navigate the tree to the server of interest
3) Double click the server to connect and open
4) Double click the Databases collection to open it
5) Right click the source database
6) Click on All Tasks | Generate SQL Script...
7) Click on the Show All button in the upper right corner
8) Click in the Script all objects checkbox
9) Click the Options tab
10) In the security section:
11) Click the checkbox for Script database users and database roles
12) Click the checkbox for Script object-level permissions
13) In the Table Scripting section:
14) Click all four checkboxes
15) Click the Ok button
16) Make the appropriate choices for saving the file

This should get you a script that includes everything, in the correct order to rebuild the schema from scratch. You should be able to edit this script to change CHAR to NCHAR and VARCHAR to NVARCHAR without any problems (or at least I can't think of any).

While you can hunt down all of the "problem child" columns and fix them as you did in your example, it is a lot more work and I'm not completely comfortable that you'll get what you really want, especially from a performance standpoint.

-PatP|||Hello there,
I'm sorry but it just doesn't seem to work :-(
Everytime I try to execute the edited script in the query analyzer(against the new empty database or the source database), I get errors like a particular table/sp doesn't exist (or incorrect syntax), even though it does exist before the execution, but seems to get dropped/deleted during the execution from the source database.
Thanks for ur time, any other suggestions would be highly appreciated!

Sheetal.|||If you have foreign key definitions (look for the keyword FOREIGN to find them), you will want to move them to the end of the script. This is due to the fact that the scripting engine doesn't always respect the "dependance sequence" of the tables, so the tables aren't always created in the same order that they were originally created.

-PatP|||You may also just be able to run your script twice, ignoring any errors that state that a particular object already exists.|||You may also just be able to run your script twice, ignoring any errors that state that a particular object already exists.As long as you skip the DROPs after the first run!

-PatP|||So, the scripting was completed successfully,with everything done as specified except that in the Options tab, the MS-DOS(OEM) format was selected, and not the Windows ANSI format.It does give some errors because of dependent objects, which when moved before the creation of the calling procedure, works fine. But it can be a hassle if they are many in number(as in my case). Any workarounds this problem??

Also, an important question for me is to know how to find and replace a certain string, eg changing char to nchar while using the query analyzer's "Replace", passes thru every string named 'varchar' or 'character' as well, so its very time consuming.I've tried using '(space)char', but its not foolproof either. Is there any way I can search for regular expressions automatically, as doin it manually in a huge database script is not very practical. Like for eg, creating a .bat file and using FINDSTR? if I'm on the right track, please guide me further!
Thanks!
Sheetal.|||Let's try a different approach...

Modify the directions for step #14 to exclude the last check box for Primary, Foreign, and Check constraints. Build the script that way (without the constraints).

Build a second script with only the constraints.

1) Launch SQL Enterprise Manager
2) Navigate the tree to the server of interest
3) Double click the server to connect and open
4) Double click the Databases collection to open it
5) Right click the source database
6) Click on All Tasks | Generate SQL Script...
7) Click on the Show All button in the upper right corner
8) Click in the Script all tables checkbox
9) Click the Formatting tab
10) Clear all of the check boxes
11) Click the Options tab
12) In the Table Scripting section:
13) Click only the PRIMARY keys, FOREIGN KEYS, and check constraints checkbox
14) Click the Ok button
15) Make the appropriate choices for saving the file

Now you should be able to play the first script, then play the second script without running into the dependancy problems you've been having.

In terms of better editing tools, I'd use an editor that recognizes regular expressions (Elvis is free, there are lots of others), or a tool like Perl that was made for those kinds of tasks.

-PatP

conversion error after promoting to production box

Problem: data conversion component going from unicode DT_WSTR to DT_STR with 1252 codepage using a Source Provider=IBMDADB2.1;

So.. I developed the package on the testdb without any errors. However, when I promote the package to the production box, I get this conversion error. I thought that it was specific to one column, so I set the ignore truncation error in the data flow. The next column provided then caused the same error.

what settings can I look at changing to prevent this error?

what data is being truncated or is the unicode conversion map failing?

the max(length) is 270 on a varchar 300. I also read something about tabs in this type of conversions failing?

Why would this be specific to the box? I have checked the collation on both DB's and they are SQL_Latin1_General_CP1_CI_AS

what is the normal workaround for this? is AlwaysUseDefaultCodePage a part of the solution?

<Error>The "output column "STATUSRENEWAL" (4477)" failed because truncation occurred, and the truncation row disposition on "output column "STATUSRENEWAL" (4477)" specifies failure on truncation.

</Error>

thanks for your time.

To find out the rows causing this, redirect the failing rows to error output and save it e.g. to a file.
The problem could be caused by Unicode characters in DT_WSTR columns that can not be represented by codepage 1252. The workaround would depend on what you are going to do with this? Are you OK with loss of information - then ignore the error; if not - don't convert Unicode data to single-byte code page; or maybe you need to cleanup the source data to avoid international characters that can't be fit to codepage 1252?sqlsql