SQL Server ignoring collation from database - sql-server

So for tests I am creating temporary docker SQL Server.
Unfortunately my production database uses some non standard collation.
So for tests I am creating test database with my custom collation. In next step using flyway I am migrating schema, but it seems like even database has my custom collation, flyway SQL statements inherit collation from SQL Server instance and I am getting collation collision exception.
Is there any way to force that every statement in database x should use database collation by default?
One important thing is that many of those SQLs are already on production, so I cannot add to them collation statement explicit.

Related

SQL Server script, overwrite case sensivity at database level

My ASP.NET application is developed with a SQL Server database with a case insensitive collation. The inline SQL scripts sometimes use variables and CTEs. In a case insensitive server, the app works perfectly fine, even if sometimes the variable names mismatch capitalization.
Recently the database team migrated our database to a different server that IS case sensitive, and some of the code path started failing.
The database team can not change the server collation because an existing database requires that particular collation. Is there a way for me to set a flag or something at connection property, or anywhere, to treat the script as case insensitive? There are more than 50 data access calls, so it would be tiresome for me to check each SQL block to ensure capitalization.
Any help?
You can set your database to be a Partially Contained Database, which will allow you to use Contained Database Collations, which completely isolate you from the instance collation, and even allow you to use a case-sensitive collation for your data, without having a case-sensitive collation for your catalog, or code.

Query changes collation language of mssql server

is there any query that changes collation language not only database but also mssql server So that databases will be created in new collation language? Or do I have to establish database again?
Even if the link above is correct, I think it need some comments.
In order to change SERVER COLLATION you should rebuild system databases, and this means that after rebuild your instance will have only empty system databases.
All the information about databases previously attached to the instance will be lost. Of course you can first detach all the user databases and than re-attach them, or restore all the user databases from backups, but all your logins and linked servers will be lost.
You'll also find your msdb empty, so all your jobs, backup/restore history will be lost.
At this point may be you find it easier to re-install SQL Server with the correct collation since the result will be pretty the same: an instance with the correct collation and only empty system databases attached to it.
P.S. If you have already some databases with collation A and you change the server collation to collation B, after databases restore/re-attach they will preserve collation A. And some code using temporary tables may be break after this, it will fail with the errors "Could not resolve collation conflict"

Collation at server level is causing issues with case sensitivity in database procedures

I am in the process of installing a database onto a client's server. The collation that is set on the server is SQL_Latin1_General_CP1_CS_AS, so it is case-sensitive.
The database that I am installing uses collation level SQL_Latin1_General_CP1_CI_AS, so it is case-insensitive.
The issue I am running into now is that all variables in stored procedures must be case sensitive or else it will throw an error.
Example:
Declare #Dimension varchar(45)
Set #dimension = 'Test'
Error:
Must declare the scalar variable "#dimension".
The lowercase "d" in the #dimension variable is causing it to be recognized as a completely different variable.
Is there a setting in the database that I can update to ignore the Server's collation?
Note: I received permission to update the collation at the server level as it is a test server. However, it is a more involved process. I am looking for a way to get around this without having to go through the steps found in this link:
Make sure you have all the information or scripts needed to re-create your user databases and all the objects in them.
Export all your data using a tool such as the bcp Utility. For more information, see Bulk Import and Export of Data (SQL Server).
Drop all the user databases.
Rebuild the master database specifying the new collation in the SQLCOLLATION property of the setup command. For example:
Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=InstanceName
/SQLSYSADMINACCOUNTS=accounts /[ SAPWD= StrongPassword ]
/SQLCOLLATION=CollationName
Create all the databases and all the objects in them.
Import all your data.
Thank you
To work around this, set your database as Partially Contained. This will give you Contained Database Collation, which provides:
Since a design objective of contained databases is to make them
self-contained, the dependence on the instance and tempdb collations
must be severed. To do this, contained databases introduce the concept
of the catalog collation. The catalog collation is used for system
metadata and transient objects.
In a contained database, the catalog collation
Latin1_General_100_CI_AS_WS_KS_SC. This collation is the same for all
contained databases on all instances of SQL Server and cannot be
changed.
As per experience would be a faster way is to backup all databases then re-install the sql-server with the correct collation, then restore all databases. Might be a manual thing for permissions but there can be other options.

User Defined Data Types with COLLATION

I am trying to find a way of specifying a COLLATION for a user-defined data type, but it doesn't appear to support collations in SQL Server, and it basically takes the collation from the database.
Is there any way of applying a collation against a user defined data type that doesn't inherit from the database collation?
I am writing a database comparison tool, using the AdventureWorks2012 database for testing, and after generate an SQL diff script and running it on a blank database the tool is now reporting a lot of differences due to the collation being different to what was actually expected.
I could create the blank database with the same collation as the source database, but I need to consider situations where organisations, etc. may wish to change the collation on the database and my tool will not work consistently and will keep on reporting differences.
Thanks.

how to change the master database collation in sql server?

is there any way to change the master database collation in MsSql server 2008 to another collation, instead of reinstall it ?
You need to rebuild/recreate the master database as described here.
(Though that article also goes through the steps to change all user databases to the new collation you probably just need to follow the steps here)

Resources