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

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.

Related

Find which tables are used by a set of queries

I have a database with many tables and a set of queries which are going to be used for a particular application. The queries only use a small subset of the tables in the database and it has been decided to create a new database with just the set of tables which are referenced by the set of queries.
Is there a way to list the tables which are used by a set of queries?
The table names in this case are usually enclosed in square brackets so I can get so far by searching on this e.g. using 'Find In Files' in SQL Server management Studio. Is there a better way, perhaps using the fact that table names are usually preceded by the word 'FROM' or 'JOIN' ?
If these queries are stored procedures, you can right click on them in SSMS and select View Dependencies.
Once selected, the Object Dependencies dialog will show, and you will want to toggle the radio button to "Objects on which [Stored procedure Name] depends.
Another option is to generate an estimated query plan, and parse the XML that it generates.

SQL Server Collation And Case Insensitive Identifiers

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.

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

Querying for tables & columns named as keywords

Assume SQL Server 2005+.
Part A:
What is the canonical way to query from the system/internal/meta/whatever tables/views (sorry, not a database ninja) for any user table or column names that use SQL Server keywords (like case)?
I don't mind maintaining the list of keywords if that's not query-able, as it only changes with versions of SQL Server supported (right?).
Looking at available views in SQL Server 2005, I can easily enough query this information from INFORMATION_SCHEMA.COLUMNS and INFORMATION_SCHEMA.TABLES, but I want to be sure it's from the best possible location for future-proofing.
Part B:
Is it possible to get the list of keywords via query?
UPDATE: While a useful concept, I'm specifically not interested in escaping the column/table/etc names in question because I'm hoping to write a tool that will check for tables/columns/etc that share names with keywords and provide useful warnings to developers. The tool would be used during code reviews at my office to point out that the developer might want to consider renaming the entity. (Or hopefully by the developer before code reviews for their own good!) I may even set it up for use with continuous integration in my build scripts, but that's only a thought for the future.
You should properly quote the names used. If you generate code, use the built-in QUOTENAME function. Don't build a list of known keywords, instead quote every name used for every object, including database name, schema name and object name. Also make sure you always adhere to the correct case of the objects involved. As a best practice, develop on a case sensitive collation server instance. Developing code on case insensitive server collation (default) can lead to embarasing failures on production when deployed on case sensitive collation servers.
For Part A
Personally I would go for sys.columns and sys.objects actually. INFORMATION_SCHEMA views are also good, and they're 'portable' in theory, I'm just so much more used to the SQL specific ones though. I choose sys.objects vs. sys.tables because it covers more (eg. views). I would suggest you also cover table valued functions, table valued parameter types (in 2008 only) and temporary #tables and table #variables declared inside stored procedures. That would leave out only temp #tables and table #variables declared in batches sent by clients, but those are basically in client code only.
A: Just use brackets around your identifier.
select [procedure].[case] from [procedure]
B: I'm not sure if you can query for them, but there is a MSDN page about it.
If you need these programmatically, I suggest you insert them all into a table for your own uses.
Why do you need to know the list of keywords? a: they don't change very often, and b: for any regular code (I'm excluding things like "sql server management studio") you can just use square brackets:
SELECT [table].[column], [table].[join]
FROM [table]