Skip to content

Commit 209b83f

Browse files
authored
Merge pull request #21120 from LitKnd/contenthealthq3
Content Health Q3 updates
2 parents c0afbb2 + bd4d191 commit 209b83f

3 files changed

Lines changed: 172 additions & 179 deletions

File tree

Lines changed: 67 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
description: "Set or Change the Database Collation"
3-
title: "Set or Change the Database Collation | Microsoft Docs"
2+
description: "Set or change the database collation"
3+
title: "Set or change the database collation"
44
ms.custom: ""
5-
ms.date: "10/27/2020"
5+
ms.date: "02/03/2022"
66
ms.prod: sql
77
ms.reviewer: ""
88
ms.technology:
@@ -15,63 +15,50 @@ author: WilliamDAssafMSFT
1515
ms.author: wiassaf
1616
monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
1717
---
18-
# Set or Change the Database Collation
19-
[!INCLUDE [SQL Server](../../includes/applies-to-version/sqlserver.md)]
20-
This topic describes how set and change the database collation by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. If no collation is specified, the server collation is used.
21-
22-
> [!IMPORTANT]
23-
> `ALTER DATABASE COLLATE` statement on Azure SQL Database is not supported.
18+
# Set or change the database collation
2419

25-
**In This Topic**
26-
27-
- **Before you begin:**
28-
29-
[Limitations and Restrictions](#Restrictions)
30-
31-
[Recommendations](#Recommendations)
32-
33-
[Security](#Security)
34-
35-
- **To set or change the database collation, using:**
36-
37-
[SQL Server Management Studio](#SSMSProcedure)
38-
39-
[Transact-SQL](#TsqlProcedure)
40-
41-
## <a name="BeforeYouBegin"></a> Before You Begin
20+
[!INCLUDE [SQL Server, Azure SQL Database Managed Instance](../../includes/applies-to-version/sql-asdbmi.md)]
21+
22+
This article describes how to set or change the database collation by using [SQL Server Management Studio (SSMS)](../../ssms/sql-server-management-studio-ssms.md) or [!INCLUDE[tsql](../../includes/tsql-md.md)]. If no collation is specified, the [server collation](set-or-change-the-server-collation.md) is used.
4223

43-
### <a name="Restrictions"></a> Limitations and Restrictions
24+
## <a name="Restrictions"></a> Limitations and restrictions
4425

45-
- Windows Unicode-only collations can only be used with the COLLATE clause to apply collations to the **nchar**, **nvarchar**, and **ntext** data types on column level and expression-level data. They cannot be used with the COLLATE clause to change the collation of a database or server instance.
26+
- Windows Unicode-only collations can only be used with [the COLLATE clause](../../t-sql/statements/collations.md) to apply collations to the **nchar**, **nvarchar**, and **ntext** data types on column level and expression-level data. They can’t be used with the COLLATE clause to change the collation of a database or server instance.
4627

47-
- If the specified collation or the collation used by the referenced object uses a code page that is not supported by Windows, the [!INCLUDE[ssDE](../../includes/ssde-md.md)] displays an error.
28+
- If the specified collation or the collation used by the referenced object uses a code page that isn’t supported by Windows, the [!INCLUDE[ssDE](../../includes/ssde-md.md)] displays an error.
29+
30+
- Server-level collation in Azure SQL Managed Instance can be specified when the instance is created and cannot be changed later. Learn more in [Set or change the server collation](set-or-change-the-server-collation.md#setting-the-server-collation-in-managed-instance).
31+
32+
> [!IMPORTANT]
33+
> The `ALTER DATABASE COLLATE` statement is not supported on Azure SQL Database.
4834
49-
### <a name="Recommendations"></a> Recommendations
35+
## <a name="Recommendations"></a> Recommendations
5036

5137
You can find the supported collation names in [Windows Collation Name &#40;Transact-SQL&#41;](../../t-sql/statements/windows-collation-name-transact-sql.md) and [SQL Server Collation Name &#40;Transact-SQL&#41;](../../t-sql/statements/sql-server-collation-name-transact-sql.md); or you can use the [sys.fn_helpcollations &#40;Transact-SQL&#41;](../../relational-databases/system-functions/sys-fn-helpcollations-transact-sql.md) system function.
5238

53-
When you change the database collation, you change the following:
54-
55-
- Any **char**, **varchar**, **text**, **nchar**, **nvarchar**, or **ntext** columns in system tables are changed to the new collation.
39+
When you change the database collation, you change:
5640

57-
- All existing **char**, **varchar**, **text**, **nchar**, **nvarchar**, or **ntext** parameters and scalar return values for stored procedures and user-defined functions are changed to the new collation.
41+
- Any **char**, **varchar**, **text**, **nchar**, **nvarchar**, or **ntext** columns in system tables are changed to the new collation.
5842

59-
- The **char**, **varchar**, **text**, **nchar**, **nvarchar**, or **ntext** system data types, and all user-defined data types based on these system data types, are changed to the new default collation.
43+
- All existing **char**, **varchar**, **text**, **nchar**, **nvarchar**, or **ntext** parameters and scalar return values for stored procedures and user-defined functions are changed to the new collation.
44+
45+
- The **char**, **varchar**, **text**, **nchar**, **nvarchar**, or **ntext** system data types, and all user-defined data types based on these system data types, are changed to the new default collation.
6046

6147
You can change the collation of any new objects that are created in a user database by using the `COLLATE` clause of the [ALTER DATABASE](../../t-sql/statements/alter-database-transact-sql.md) statement. This statement **does not change** the collation of the columns in any existing user-defined tables. These can be changed by using the `COLLATE` clause of [ALTER TABLE](../../t-sql/statements/alter-table-transact-sql.md).
6248

6349
> [!IMPORTANT]
64-
> Changing the collation of a database or individual columns **does not modify** the underlying data already stored in existing tables. Unless your application explicitly handles data conversion and comparison between different collations, it is recommended that you transition existing data in the database to the new collation. This removes the risk that applications may incorrectly modify data, resulting in possible wrong results or silent data loss.
50+
> Changing the collation of a database or individual columns **does not modify** the underlying data already stored in existing tables. Unless your application explicitly handles data conversion and comparison between different collations, it is recommended that you transition existing data in the database to the new collation. This removes the risk that applications may incorrectly modify data, resulting in possible wrong results or silent data loss.
6551
6652
When a database collation is changed, only new tables will inherit the new database collation by default. There are several alternatives to convert existing data to the new collation:
67-
- Convert data in-place. To convert the collation for a column in an existing table, see [Set or Change the Column Collation](../../relational-databases/collations/set-or-change-the-column-collation.md). This operation is easy to implement, but may become a blocking issue for large tables and busy aplications. See the following example for an in-place conversion of the `MyString` column to a new collation:
53+
54+
- Convert data in-place. To convert the collation for a column in an existing table, see [Set or Change the Column Collation](../../relational-databases/collations/set-or-change-the-column-collation.md). This operation is easy to implement, but may become a blocking issue for large tables and busy applications. See the following example for an in-place conversion of the `MyString` column to a new collation:
6855

6956
```sql
7057
ALTER TABLE dbo.MyTable
71-
ALTER COLUMN MyString VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8;
58+
ALTER COLUMN MyString VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8;
7259
```
7360

74-
- Copy data to new tables that use the new collation, and replace original tables in the same database. Create a new table in the current database that will inherit the database collation, copy the data between the old table and the new table, drop the original table, and rename the new table to the name of the original table. This is a faster operation than an in-place conversion, but may become a challenge when handling complex schemas with dependencies such as Foreign Key contraints, Primary Key constraints, and Triggers. It would also require a final data syncronization between the original and the new table before the final cut-off, if data continues to be changed by applications. See the following example for a "copy and replace" conversion of the `MyString` column to a new collation:
61+
- Copy data to new tables that use the new collation, and replace original tables in the same database. Create a new table in the current database that will inherit the database collation, copy the data between the old table and the new table, drop the original table, and rename the new table to the name of the original table. This is a faster operation than an in-place conversion, but may become a challenge when handling complex schemas with dependencies such as Foreign Key constraints, Primary Key constraints, and Triggers. It would also require a final data synchronization between the original and the new table before the final cut-off, if data continues to be changed by applications. See the following example for a "copy and replace" conversion of the `MyString` column to a new collation:
7562

7663
```sql
7764
CREATE TABLE dbo.MyTable2 (MyString VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8);
@@ -84,86 +71,83 @@ When a database collation is changed, only new tables will inherit the new datab
8471
EXEC sp_rename 'dbo.MyTable2', 'dbo.MyTable';
8572
```
8673

87-
- Copy data to a new database that uses the new collation, and replace the original database. Create a new database using the new collation, and transfer the data from the original database via tools like [!INCLUDE[ssISnoversion](../../includes/ssisnoversion-md.md)] or the Import/Export Wizard in [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. This is a simpler approach for complex schemas. It would also require a final data syncronization between the original and the new databases before the final cut-off, if data continues to be changed by applications.
74+
- Copy data to a new database that uses the new collation, and replace the original database. Create a new database using the new collation, and transfer the data from the original database via tools like [!INCLUDE[ssISnoversion](../../includes/ssisnoversion-md.md)] or the Import/Export Wizard in [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. This is a simpler approach for complex schemas. It would also require a final data synchronization between the original and the new databases before the final cut-off, if data continues to be changed by applications.
8875

89-
### <a name="Security"></a> Security
90-
91-
#### <a name="Permissions"></a> Permissions
76+
## <a name="Permissions"></a> Permissions
9277
To create a new database, requires `CREATE DATABASE` permission in the **master** database, or requires `CREATE ANY DATABASE`, or `ALTER ANY DATABASE` permission.
9378

9479
To change the collation of an existing database, requires `ALTER` permission on the database.
9580

96-
## <a name="SSMSProcedure"></a> Using SQL Server Management Studio
97-
98-
#### To set or change the database collation
99-
100-
1. In **Object Explorer**, connect to an instance of the [!INCLUDE[ssDEnoversion](../../includes/ssdenoversion-md.md)], expand that instance, and then expand **Databases**.
101-
102-
2. If you are creating a new database, right-click **Databases** and then click **New Database**. If you do not want the default collation, click the **Options** page, and select a collation from the **Collation** drop-down list.
81+
## <a name="SSMSProcedure"></a> Set or change the database collation using SSMS
10382

104-
Alternatively, if the database already exists, right-click the database that you want and click **Properties**. Click the **Options** page, and select a collation from the **Collation** drop-down list.
83+
1. In **Object Explorer**, connect to an instance of the [!INCLUDE[ssDEnoversion](../../includes/ssdenoversion-md.md)], expand that instance, and then expand **Databases**.
10584

106-
3. After you are finished, click **OK**.
85+
1. If you are creating a new database, right-click **Databases** and then select **New Database**. If you don’t want the default collation, select the **Options** page, and select a collation from the **Collation** drop-down list.
10786

108-
## <a name="TsqlProcedure"></a> Using Transact-SQL
87+
Alternatively, if the database already exists, right-click the database that you want and select **Properties**. Select the **Options** page, and select a collation from the **Collation** drop-down list.
10988

110-
#### To set the database collation
89+
1. After you are finished, select **OK**.
11190

112-
1. Connect to the [!INCLUDE[ssDE](../../includes/ssde-md.md)].
91+
## <a name="TsqlProcedure"></a> Set the database collation using Transact-SQL
11392

114-
2. From the Standard bar, click **New Query**.
93+
1. Connect to the [!INCLUDE[ssDE](../../includes/ssde-md.md)].
11594

116-
3. Copy and paste the following example into the query window and click **Execute**. This example shows how to use the [COLLATE](~/t-sql/statements/collations.md) clause to specify a collation name. The example creates the database `MyOptionsTest` that uses the `Latin1_General_100_CS_AS_SC` collation. After you create the database, execute the `SELECT` statement to verify the setting.
95+
1. From the Standard bar, select **New Query**.
11796

97+
1. Copy and paste the following example into the query window and select **Execute**. This example shows how to use the [COLLATE clause](../../t-sql/statements/collations.md) in [CREATE DATABASE](../../t-sql/statements/create-database-transact-sql.md) to specify a collation name. The example creates the database `MyOptionsTest` that uses the `Latin1_General_100_CS_AS_SC` collation. After you create the database, execute the `SELECT` statement to verify the setting.
98+
11899
```sql
119100
USE master;
120-
GO
101+
GO
102+
121103
IF DB_ID (N'MyOptionsTest') IS NOT NULL
122-
DROP DATABASE MyOptionsTest;
123-
GO
104+
DROP DATABASE MyOptionsTest;
105+
GO
106+
124107
CREATE DATABASE MyOptionsTest
125-
COLLATE Latin1_General_100_CS_AS_SC;
108+
COLLATE Latin1_General_100_CS_AS_SC;
126109
GO
127110

128-
--Verify the collation setting.
129111
SELECT name, collation_name
130112
FROM sys.databases
131113
WHERE name = N'MyOptionsTest';
132114
GO
133115
```
116+
117+
## Change the database collation using Transact-SQL
134118

135-
#### To change the database collation
136-
137-
1. Connect to the [!INCLUDE[ssDE](../../includes/ssde-md.md)].
119+
1. Connect to the [!INCLUDE[ssDE](../../includes/ssde-md.md)].
138120

139-
2. From the Standard bar, click **New Query**.
121+
1. From the Standard bar, select **New Query**.
140122

141-
3. Copy and paste the following example into the query window and click **Execute**. This example shows how to use the [COLLATE](~/t-sql/statements/collations.md) clause in an [ALTER DATABASE](../../t-sql/statements/alter-database-transact-sql.md) statement to change the collation name. Execute the `SELECT` statement to verify the change.
123+
1. Copy and paste the following example into the query window and select **Execute**. This example shows how to use the [COLLATE clause](../../t-sql/statements/collations.md) in an [ALTER DATABASE](../../t-sql/statements/alter-database-transact-sql.md) statement to change the collation name. Execute the `SELECT` statement to verify the change.
142124

143125
```sql
144126
USE master;
145-
GO
127+
GO
128+
146129
ALTER DATABASE MyOptionsTest
147-
COLLATE French_CI_AS ;
130+
COLLATE French_CI_AS ;
148131
GO
149132

150-
--Verify the collation setting.
151133
SELECT name, collation_name
152134
FROM sys.databases
153135
WHERE name = N'MyOptionsTest';
154136
GO
155137
```
156138

157-
## See Also
158-
[Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md)
159-
[sys.fn_helpcollations &#40;Transact-SQL&#41;](../../relational-databases/system-functions/sys-fn-helpcollations-transact-sql.md)
160-
[sys.databases &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md)
161-
[SQL Server Collation Name &#40;Transact-SQL&#41;](../../t-sql/statements/sql-server-collation-name-transact-sql.md)
162-
[Windows Collation Name &#40;Transact-SQL&#41;](../../t-sql/statements/windows-collation-name-transact-sql.md)
163-
[COLLATE &#40;Transact-SQL&#41;](~/t-sql/statements/collations.md)
164-
[Collation Precedence &#40;Transact-SQL&#41;](../../t-sql/statements/collation-precedence-transact-sql.md)
165-
[CREATE TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/create-table-transact-sql.md)
166-
[CREATE DATABASE &#40;SQL Server Transact-SQL&#41;](../../t-sql/statements/create-database-transact-sql.md)
167-
[ALTER TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-table-transact-sql.md)
168-
[ALTER DATABASE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-database-transact-sql.md)
169-
139+
## Next steps
140+
141+
Learn more about collation:
142+
143+
- [Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md)
144+
- [sys.fn_helpcollations &#40;Transact-SQL&#41;](../../relational-databases/system-functions/sys-fn-helpcollations-transact-sql.md)
145+
- [sys.databases &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md)
146+
- [SQL Server Collation Name &#40;Transact-SQL&#41;](../../t-sql/statements/sql-server-collation-name-transact-sql.md)
147+
- [Windows Collation Name &#40;Transact-SQL&#41;](../../t-sql/statements/windows-collation-name-transact-sql.md)
148+
- [COLLATE &#40;Transact-SQL&#41;](~/t-sql/statements/collations.md)
149+
- [Collation Precedence &#40;Transact-SQL&#41;](../../t-sql/statements/collation-precedence-transact-sql.md)
150+
- [CREATE TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/create-table-transact-sql.md)
151+
- [CREATE DATABASE &#40;SQL Server Transact-SQL&#41;](../../t-sql/statements/create-database-transact-sql.md)
152+
- [ALTER TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-table-transact-sql.md)
153+
- [ALTER DATABASE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-database-transact-sql.md)

0 commit comments

Comments
 (0)