سؤال

I need to change all the varchar columns to the nvarchar type. I've generated a script with the help from this thread SQL Server - script to update database columns from varchar to nvarchar if not already nvarchar.

However this script cannot change the composite primary key columns. We have many composite keys in our table so this cannot be done manually.

Is there any way to script the change from varchar composite keys to nvarchar?

هل كانت مفيدة؟

المحلول

Do you have a build process for your databases? Do you have a Source Control system? If you do then I would suggest you make the changes by search and replace in source control, rebuild the database and then use a schema comparison tool (as in Visual Studio Database Projects or RedGate Schema Compare) to generate a change script.

If you don't have a build process or source control then you can script your database using Management Studio (right-click the database, select All Tasks > Generate Scripts).

نصائح أخرى

I decided to do this partially manually. First I manually changed all the primary keys from varchar to nvarchar (using Management Studio). Then I used the script to change all the other (non-constrainted) columns to nvarchar. After that I changed all the views, stored procs and triggers using find-replace. Finally I compared the database with the production one using Red Gate SQL Compare.

I also had to split the final script to smaller parts, because some of the tables were too large to be rebuilt at the same time (transaction log filled the entire drive).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top