首页 > 其他分享 >How I can obtain the collation of a specific table in a database?

How I can obtain the collation of a specific table in a database?

时间:2022-11-14 14:24:23浏览次数:91  
标签:Latin1 database obtain specific SQL collation table your

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;

SqlFiddle here

 

 

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:

C#
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:

C#
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

相关文章

  • 解决操作mysql报 1044 - Access denied for user 'root'@'%' to database 'table&#039
    naccat操作数据库时报 1044-Accessdeniedforuser'root'@'%'todatabase'table'说明root用户没有授权,需要授权Mysql8.01.进入mysql容器dockerexec-itmys......
  • Specific static library / shared library / header path on OSX
    StaticlibrarypathexportLIBRARY_PATH="/usr/local/lib"SharedlibraryordynamiclibrarypathexportDYLD_LIBRARY_PATH="/usr/local/lib"Headerfilepathex......
  • DataBase - 查询 SQL 进阶
    查询每个部门工资最高的员工信息。SELECT e.emp_name,d.dept_name,e.salaryFROM empeinnerjoindeptdONe.dept_id=d.dept_idWHERE (e.dept_id,e.salary)......
  • 规约 Specification
    第一次看到这个词 Specification是在ABP框架中,起先没怎么太关注这个。随着接触DDD领域驱动越来越来多,关于如何集中业务逻辑的问题,再次浮现在我脑中。好吧,我不知道。我......
  • [15-445]Database Storage2 related memo
    Storage1主要介绍了slotted-page组织数据的情况。但是这种方式会有一些问题比如1.页分裂(比如在一个页上面操作,后续对其进行操作可能会有删除的操作后续可能需要使用......
  • [15-445]Database Storage related memo 1
    最先的一部分还是介绍存储介质速度层级 总的来说就是cpu>memory>disk但是究竟快多少呢?  我觉得这里只需要记住一个常用的关键论点,内存约比ssd快150倍......
  • IDEA成功连接Database但不显示表怎么办
    IntelliJIDEA中用database来连接数据库时,换了新环境,有时候会出现连接成功但是数据库连接下没有user或者是没有table表。这里以idea2020.2.3为例,进行问题解决。  ......
  • database
    数据库MySQL的逻辑架构第一层是服务器层,主要提供连接处理、授权认证、安全等功能第二层实现了MySQL核心服务功能,包括查询解析、分析、优化、缓存以及日期和时间等所有......
  • ArcGIS文件地理数据库体积、名字等的限值 File geodatabase size and name limits
    sorry,Imjustacarrier.Filegeodatabasesize: NolimitTableorfeatureclasssize: 1TB(default),4GBor256TBwithkeywordNumberoffeatureclassesa......
  • Excel VBA中ADO连接mysql数据库报错:no database selected 解决方案
    几天翻到一个以前连接mysql数据库的Excelvba文件,试了一下,居然链接成功,但是执行sql的时候一直报错说什么nodatabaseselected百度谷歌了老半天,终于找到了解决方案,其......