You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
24
19
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:**
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.
42
23
43
-
### <aname="Restrictions"></a> Limitations and Restrictions
24
+
##<aname="Restrictions"></a> Limitations and restrictions
44
25
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.
46
27
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.
48
34
49
-
### <aname="Recommendations"></a> Recommendations
35
+
## <aname="Recommendations"></a> Recommendations
50
36
51
37
You can find the supported collation names in [Windows Collation Name (Transact-SQL)](../../t-sql/statements/windows-collation-name-transact-sql.md) and [SQL Server Collation Name (Transact-SQL)](../../t-sql/statements/sql-server-collation-name-transact-sql.md); or you can use the [sys.fn_helpcollations (Transact-SQL)](../../relational-databases/system-functions/sys-fn-helpcollations-transact-sql.md) system function.
52
38
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:
56
40
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.
58
42
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.
60
46
61
47
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).
62
48
63
49
> [!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.
65
51
66
52
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:
68
55
69
56
```sql
70
57
ALTERTABLEdbo.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;
72
59
```
73
60
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:
@@ -84,86 +71,83 @@ When a database collation is changed, only new tables will inherit the new datab
84
71
EXEC sp_rename 'dbo.MyTable2', 'dbo.MyTable';
85
72
```
86
73
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.
88
75
89
-
### <aname="Security"></a> Security
90
-
91
-
#### <aname="Permissions"></a> Permissions
76
+
## <aname="Permissions"></a> Permissions
92
77
To create a new database, requires `CREATE DATABASE` permission in the **master** database, or requires `CREATE ANY DATABASE`, or `ALTER ANY DATABASE` permission.
93
78
94
79
To change the collation of an existing database, requires `ALTER` permission on the database.
95
80
96
-
## <aname="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
+
## <aname="SSMSProcedure"></a> Set or change the database collation using SSMS
103
82
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**.
105
84
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.
107
86
108
-
## <aname="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.
109
88
110
-
#### To set the database collation
89
+
1. After you are finished, select **OK**.
111
90
112
-
1. Connect to the [!INCLUDE[ssDE](../../includes/ssde-md.md)].
91
+
## <aname="TsqlProcedure"></a> Set the database collation using Transact-SQL
113
92
114
-
2. From the Standard bar, click **New Query**.
93
+
1. Connect to the [!INCLUDE[ssDE](../../includes/ssde-md.md)].
115
94
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**.
117
96
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
+
118
99
```sql
119
100
USE master;
120
-
GO
101
+
GO
102
+
121
103
IF DB_ID (N'MyOptionsTest') IS NOT NULL
122
-
DROPDATABASE MyOptionsTest;
123
-
GO
104
+
DROPDATABASE MyOptionsTest;
105
+
GO
106
+
124
107
CREATEDATABASEMyOptionsTest
125
-
COLLATE Latin1_General_100_CS_AS_SC;
108
+
COLLATE Latin1_General_100_CS_AS_SC;
126
109
GO
127
110
128
-
--Verify the collation setting.
129
111
SELECT name, collation_name
130
112
FROMsys.databases
131
113
WHERE name = N'MyOptionsTest';
132
114
GO
133
115
```
116
+
117
+
## Change the database collation using Transact-SQL
134
118
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)].
138
120
139
-
2.From the Standard bar, click**New Query**.
121
+
1.From the Standard bar, select**New Query**.
140
122
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.
142
124
143
125
```sql
144
126
USE master;
145
-
GO
127
+
GO
128
+
146
129
ALTERDATABASE MyOptionsTest
147
-
COLLATE French_CI_AS ;
130
+
COLLATE French_CI_AS ;
148
131
GO
149
132
150
-
--Verify the collation setting.
151
133
SELECT name, collation_name
152
134
FROMsys.databases
153
135
WHERE name = N'MyOptionsTest';
154
136
GO
155
137
```
156
138
157
-
## See Also
158
-
[Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md)
0 commit comments