Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2004
    Posts
    5

    Unanswered: 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.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

  3. #3
    Join Date
    Apr 2004
    Posts
    5

    Conversion from char to Nchar

    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.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

  5. #5
    Join Date
    Apr 2004
    Posts
    5

    Conversion from char to Nchar

    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.

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

  7. #7
    Join Date
    Apr 2004
    Posts
    5

    Conversion from char to Nchar

    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.

  8. #8
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

  9. #9
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    You may also just be able to run your script twice, ignoring any errors that state that a particular object already exists.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  10. #10
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by blindman
    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

  11. #11
    Join Date
    Apr 2004
    Posts
    5

    Conversion from char to Nchar

    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.

  12. #12
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •