SQL Server Collation And Case Insensitive Identifiers - sql

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.

Related

Collation error - SQL Server

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.

Yii - Case-sensitivity of the table name, column in sql query

How to get rid of case-sensitive dependence in database query in Yii, using different ways to access the database, either through Active Record, DAO, and through the query designer.
First of all to avoid case sensitive dependence in database query, you need to make the database case insensitive by setting a flag in configuration file as in MySQL, Which will make all database queries case insensitive

applying collation to sql server databases

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.

Setting and Changing the SQL Server Collation

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.

Case-sensitive database collation, but case-insensitive SQL queries

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.