简体   繁体   中英

Changing DB Collation in VS2010 DB Project

I have a DB project in SQL Server 2008 R2. I have a table within this project, eg:

CREATE TABLE [dbo].[MyTable](
    [IDField] [int] IDENTITY(1,1) NOT NULL,
    [AnotherField] [int] NULL,
    [StrangeBehaviour] [varchar](50) NULL,
 CONSTRAINT [PK_MyID] PRIMARY KEY CLUSTERED 
(
    [IDField] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

The table script looks like this. When I deploy the project using vsdbcmd.exe it creates / updates the table as expected.

However, in SSMS in the object explorer, if I select the table column "StrangeBehaviour" on the target DB and select Properties or Modify then I see that the collation is being set to something other than the DB default (the default being Windows Collation -> Latin1_General. This is then set to a seemingly random windows collation.

If I script the table as "Insert To" in SSMS then I don't see the changed collation.

I therefore have two questions:

  • I was under the impression that the upgrade script was based on the SQL inside the DB project - is this not the case and, if not, how do I access the additional data?

  • How is this property held in the target database when none of the scripts seem to reference the changed collation?

There is a database collation property in the database project under Properties > Database.sqlsettings . The script generated when deploying from Visual Studio or VSDBCMD will change the default collation to the one specified in this setting if it doesn't match already.

Then the collation of columns created by the deployment from Visual Studio will be based on this setting.

I've tested this by creating a new database with default collation Latin1_General_CI_AS . I then created a new database project with the default collation setting set to SQL_Latin1_General_CP1_CI_AS . This is the deployment script that Visual Studio creates:

...
ALTER DATABASE [$(DatabaseName)] COLLATE SQL_Latin1_General_CP1_CI_AS;


GO
USE [$(DatabaseName)]
GO
/*
 Pre-Deployment Script Template
...
*/

GO
PRINT N'Creating [dbo].[MyTable]...';


GO
CREATE TABLE [dbo].[MyTable] (
    [IDField]          INT          IDENTITY (1, 1) NOT NULL,
    [AnotherField]     INT          NULL,
    [StrangeBehaviour] VARCHAR (50) NULL,
    CONSTRAINT [PK_MyID] PRIMARY KEY CLUSTERED ([IDField] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF) ON [PRIMARY]
);


GO
...

Within SQL Server Management Studio (SSMS) there is an option to automatically generate scripts for your changes done through the GUI. The solution for this problem is provided in SSMS and works with both SQL Server 2005 and SQL Server 2008.

To enable the option

From the SSMS menus click on "Tools"

Click on "Options..."

Click on "Designers"

Check the checkbox "Auto generate change scripts"

I got similar issue as in my keyboard I have uppcase i with dot: İ (Turkish i)

Probably this case:

your vsdbcmd.exe tool takes collalition from windows collation.

It is not random, check your windows your collation from:

http://msdn.microsoft.com/en-us/library/aa176553%28v=sql.80%29.aspx

and here is a great link about windows collation vs sql collation:

http://msdn.microsoft.com/en-us/library/ms143508.aspx

I am not sql guy, but my queries including id transted to İD

when I say Select Top 1000 rows from context menu of a sql table. As It uses windows collation if fails.

However when I say, Edit Top200 rows from context menu of a sql table, it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM