I am currently working with applying collations to my sql server databases which are housed on a database server - sql server 2012.
Each of the databases will have different collations as the data varies from Latin, to Cryillic and one Arabic database.
I have 3 questions:
Is it possible to apply a number of collations to the same sql server database?
The fact databases of different collations are on the same database server, could their potential for conflict between the collation of the server and the databases?
Finally - is there a script I can use to apply a collation to each of the databases, noting they are created and contain data?
Is it possible to apply a number of collations to the same sql server
database?
The database collation is just the default for new columns in that database. You can specify different collations all you like as long as it's for different columns. You can even specify collation in views or ad-hoc SQL.
The fact databases of different collations are on the same database
server, could their potential for conflict between the collation of
the server and the databases?
The server collation is just the default collation for a new database. Typically, it's also the collation used by master and tempdb. That means queries against temporary tables require you to explicitly specify the collation.
Finally - is there a script I can use to apply a collation to each of
the databases, noting they are created and contain data?
You can use the alter database command:
ALTER DATABASE TestDb ALTER COLLATION French_CI_AI
Note that all existing columns will keep their collation. As said, the database collation is just the default for new columns.
Related
So I have two SQL Servers at the moment, both 2012 and both running separate systems.
One of these servers runs on a collation of latin1_general_bin and the other runs with latin1_general_ci_as, ideally I'd like to shut one of these servers off and move all the systems to one place.
I know this can be done technically, however I was wondering if this was a good idea? Will I be causing myself more problems down the line?
Both databases are transactional based and are defined by external parties so cannot have their collation changed. If I were as to do this, would it be better to have the server collation set as latin1_general_bin or latin1_general_ci_as? I'm thinking that the case sensitive option would be better as queries would still run against case insensitive databases whilst case insensitive queries would need to be carefully managed
There should be no problem using databases with different collations on one server. One of our external vendors also provides a database with different collation and this has been working without issues since SQL Server 2000. However, you will have to explicitly specify the collation (or use collation database_default when you want to join tables from the different databases. From my own experience, that can have quite a performance impact. You also need to be careful when using SELECT INTO when creating a new table as this can result in mixed collation in a database or table.
Yes, SQL Server keeps the collation neatly separated with the databases. But there is a big flaw, which caused severe problems with one of our customers:
The temp table (CREATE TABLE #Tbl ...) uses the server's default collation if you do not specify the collation explicitly.
In one of our projects, especially with quite old stored Procedures created with SQL Server 2000, there was heavy usage of temp tables to store intermediate results.
The customer ran into crazy errors and it took a hell of a lot of time to track this down. He did not want to change his server (due to other databases running there). So we changed the database to his collation. After this some of our upgrade scripts did not run anymore... It ended with all databases changed to the same collation and the installation of another instance of SQL Server.
Which collation is the best for you? I don't know... This depends on your needs. Are you dealing with diacritic characters a lot (look at "_as"), do you want "test" to be equal with "TeSt" (look at "_ci")
We have an old SQL Server database with case sensitive collation, but our newer application code expects column and table names to be case insensitive.
So, we are trying to convert the database so that the column and table names will be case insensitive.
The solution so far is to create an empty DB with case insensitive collation, then generating scripts from the old database to recreate the schema in the new database (using SQL Server Studio), then exporting the data from the old database into the new database.
This almost works, but the old instance allowed for a couple of rows of data that are identical when ignoring case sensitivity and we have a unique constraint violation.
Is there a simple way to allow for SQL references to table and column names to be insensitive while column data is treated as sensitive still (without having to modify the columns individually with separate collations)?
When you create a database, the system tables that hold the object names get the collation of the database -- here you want an insensitive collation. If you then want all your data to be case sensitive still, change the database's default collation, then run your scripts.
Changing the collation will not change the collation of already created objects (including the system tables) but it will affect subsequent objects.
Without changing the column collation at all, you can override it on individual queries, for example WHERE A = 'Hi' COLLATE Latin1_General_CS_AI, but this is inefficient because it will prevent the use of indexes for seeking (as conversions are required). It's not possible to define a constraint with a collation override -- you'd have to get complicated and define it on a computed column with a separate collation.
I have several databases on a SQL Server instance. I have certain queries that extract information simultaneously these databases. It turns out that after restoring of one of the databases (but that came from another server), these queries gives a COLLATION error. I realized then that this "new" database has a different COLLATION, so that forces me to use the COLLATE for each respective column in these queries. The problem is I have many queries and it would not be practical to make this change on all. I have way to change Database COLLATION, as well as all needed columns? I already tried to change the Database but it seems that columns COLLATION remain the same...
If the collation for individual columns are not set to Database default, you should change them one by one.
I have one question, I was trying to find more information on the internet but, I'm still not sure about it.
If I have a SQL Server instance with the following default Collation example "Modern_Spanish_CI_AS" and I also have a restored databse with different collation example "Latin1_General_CI_AS".
What collation SQL Server will use? by default the Modern_Spanish... or it will use from database Latin1_General?
In the follwing link explains something about it
Setting and Changing the Server Collation
Thanks!
It will use the Latin1_General_CI_AS collation, as that's what's set in the database itself.
For clarification, whatever's set in the database will override the "default" collation for the server. The default collation is basically only used when you create a brand new database.
Changing the default collation at the server level has no effect on existing databases, you would have to change each collation individually, though that in itself will probably cause you cascading issues with any other database level objects you've created, such as stored procedures, constraints and even any dynamic SQL you're executing against the database from 3rd party applications.
SQL Server collations can be set at various levels: server, database, column, expession. If you do not set a collation at a lower level it is inherited from the level above. E.g. when creating a database the database collation will be the server's collation unless you explicitly set a collation for the DB.
And many collations will work together fine. Occasionally there can be difficulties, often of the nature of sort order being unexpected. I do not know for sure but I would expect the 2 you specify to be compatible.
So yes, your DB will (almost certainly) restore with the Latin1_General_CI_AS collation and almost certainly be fine. There is a list of compatible collations which Google should be able to help you find.
I'm running a project with SSIS and now creating new database with CS (case sensitive) collation, but all queries, stored procedures etc. were written in case-insensitive manner, so now SQL Server doesn't recognize them. I need to change table names, columns names... in queries in exact the same as they are in SQL Server.
Is it possible to have CS database with CI stored procedures, queries and so on... ?
No, case sensitivity at the database level applies to object names. You can, however, create a case insensitive database, and for each text column you create, modify the collation to a case sensitive one. I believe that that would give you the behaviour you ask for.
You can apply collation at the column-level. To do this, right click your table in SQL Server Management Studio, click 'Design' then select the column and in the Column Properties menu under the 'Table Designer' section you can choose to use something other than the database default.
Note, it will not let you change the collation on a column if its part of the primary key.