How I can obtain the collation of a specific table in a database?
回答1
Collation at the table level is on a per column basis, so it is possible to have a collation different than the database. If the collation is not defined at the column level, it defaults to the database collation setting.
SQL Server 2000:
SELECT c.name,
c.collation
FROM SYSCOLUMNS c
WHERE [id] = OBJECT_ID('your_table_name')
SQL Server 2005+:
SELECT c.name,
c.collation_name
FROM SYS.COLUMNS c
JOIN SYS.TABLES t ON t.object_id = c.object_id
WHERE t.name = 'your_table_name'
获取数据库默认的collation
SELECT CONVERT (varchar(256), SERVERPROPERTY('collation'));Temp Table collation conflict - Error : Cannot resolve the collation conflict between Latin1* and SQL_Latin1*
回答1
This happens because the collations on #tempdb.temp_po.OD1
and STR_IndentDetail.D1
are different (and specifically, note that #tempdb
is a different, system database, which is generally why it will have a default opinion for collation, unlike your own databases and tables where you may have provided more specific opinions).
Since you have control over the creation of the temp table, the easiest way to solve this appears to be to create *char columns in the temp table with the same collation as your STR_IndentDetail
table:
CREATE TABLE #temp_po(
IndentID INT,
OIndentDetailID INT,
OD1 VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS,
.. Same for the other *char columns
In the situation where you don't have control over the table creation, when you join the columns, another way is to add explicit COLLATE
statements in the DML where errors occur, either via COLLATE SQL_Latin1_General_CP1_CI_AS
or easier, using COLLATE DATABASE_DEFAULT
SELECT * FROM #temp_po t INNER JOIN STR_IndentDetail s
ON t.OD1 = s.D1 COLLATE SQL_Latin1_General_CP1_CI_AS;
OR, easier
SELECT * FROM #temp_po t INNER JOIN STR_IndentDetail s
ON t.OD1 = s.D1 COLLATE DATABASE_DEFAULT;
Collations and Case Sensitivity
Database collation
In most database systems, a default collation is defined at the database level; unless overridden, that collation implicitly applies to all text operations occurring within that database. The database collation is typically set at database creation time (via the CREATE DATABASE
DDL statement), and if not specified, defaults to a some server-level value determined at setup time. For example, the default server-level collation in SQL Server for the "English (United States)" machine locale is SQL_Latin1_General_CP1_CI_AS
, which is a case-insensitive, accent-sensitive collation. Although database systems usually do permit altering the collation of an existing database, doing so can lead to complications; it is recommended to pick a collation before database creation.
When using EF Core migrations to manage your database schema, the following in your model's OnModelCreating
method configures a SQL Server database to use a case-sensitive collation:
modelBuilder.UseCollation("SQL_Latin1_General_CP1_CS_AS");
Column collation
Collations can also be defined on text columns, overriding the database default. This can be useful if certain columns need to be case-insensitive, while the rest of the database needs to be case-sensitive.
When using EF Core migrations to manage your database schema, the following configures the column for the Name
property to be case-insensitive in a database that is otherwise configured to be case-sensitive:
modelBuilder.Entity<Customer>().Property(c => c.Name)
.UseCollation("SQL_Latin1_General_CP1_CI_AS");
标签:Latin1,database,obtain,specific,SQL,collation,table,your From: https://www.cnblogs.com/chucklu/p/16888892.html