The key_guid function in SQL Server returns the GUID of a column encryption key (CEK) or a column master key (CMK) used by Always Encrypted. You call it in a SELECT statement against the system catalog views sys.column_encryption_keys or sys.column_master_keys, for example SELECT name, key_guid FROM sys.column_encryption_keys;. That is the whole answer: it is a metadata function, not a general-purpose GUID generator, and it only works in a database where Always Encrypted keys have been created.
What You'll Need
- SQL Server 2016 or later (Always Encrypted was introduced in 2016), or Azure SQL Database.
- A database where at least one column encryption key or column master key exists. If none exist, the function returns no rows.
- Permission to view the system catalog views. Members of the
publicrole can usually read the metadata they own; to see all keys you may needVIEW ANY COLUMN ENCRYPTION KEY DEFINITIONorVIEW ANY COLUMN MASTER KEY DEFINITION, or membership in a role such asdb_owner. - A query tool such as SQL Server Management Studio (SSMS), Azure Data Studio, or
sqlcmd.
Menu labels and view names can differ slightly between SQL Server versions and tools, but the catalog view names below are stable across supported releases.
Step-by-Step
- Connect to the right database. Open SSMS or Azure Data Studio, connect to your instance, and in the Object Explorer expand Databases and select the database that contains the Always Encrypted keys. In a query window, run
USE YourDatabaseName;so you are not queryingmasterby mistake. - Confirm keys exist. Run
SELECT name FROM sys.column_master_keys;andSELECT name FROM sys.column_encryption_keys;. If both return rows, the function has something to report. If they return nothing, there is nothing forkey_guidto return yet. - Query the column encryption key GUID. Run:
SELECT name, key_guid FROM sys.column_encryption_keys;
Thekey_guidcolumn is the unique identifier SQL Server stores for each CEK. You will see one row per encryption key. - Query the column master key GUID. Run:
SELECT name, key_guid FROM sys.column_master_keys;
This returns the GUID for each CMK, which is the key stored in your key store (for example Windows Certificate Store or Azure Key Vault). - Join the two views when you need the relationship. A CEK is protected by one or more CMKs. To list which master key encrypts which column key, run:
SELECT cek.name AS column_key, cmk.name AS master_key, cek.key_guid AS cek_guid, cmk.key_guid AS cmk_guid FROM sys.column_encryption_keys cek JOIN sys.column_encryption_key_values cekv ON cek.column_encryption_key_id = cekv.column_encryption_key_id JOIN sys.column_master_keys cmk ON cekv.column_master_key_id = cmk.column_master_key_id;
This is the practical use ofkey_guid: matching a key name to the GUID that appears in scripts, deployment tools, and error messages. - Use the GUID in follow-up scripts. Copy the GUID value and use it in your own metadata queries, for example to filter
sys.columnsfor encrypted columns that reference a particular CEK. Do not hard-code GUIDs into application connection strings; they are identifiers for administration, not secrets. - Verify the result. Compare the returned GUID with the value shown in the Always Encrypted wizard or in your key store entry. A mismatch usually means you are connected to a different database or a restored copy where keys were recreated.
Common Mistakes
- Expecting a new GUID.
key_guiddoes not generate a GUID; it reads the GUID already assigned to a key. For a new value, useNEWID()orNEWSEQUENTIALID()instead. - Running it in the wrong database. The views are database-scoped. Querying
masterreturns nothing even when keys exist elsewhere. - Confusing CEK and CMK GUIDs. They are different identifiers from different views. Always label which one you are selecting.
- Assuming it works on older versions. Always Encrypted and these catalog views are not available on SQL Server 2014 or earlier.
- Treating the GUID as a secret. The GUID is metadata. The actual key material lives in the key store and is never exposed by this function.
Tips
- Alias the column (
key_guid AS KeyId) so results are easier to read in grid output. - Use
ORDER BY namewhen listing many keys to keep results predictable. - Save the join query from step 5 as a snippet; it answers most “which key protects which column” questions.
- When restoring a database to another server, re-check the GUIDs; keys may need to be recreated if the master key is unavailable on the target.
- Remember that permissions on the catalog views are separate from permissions on the encrypted data itself.
Troubleshooting
- Empty result set. Check the database context first, then confirm keys exist with the queries in step 2. If the database was restored without the master key, the key rows may be missing.
- “Invalid object name” error. You are likely on a version that does not support Always Encrypted, or you typed the view name incorrectly. Verify the server version and spelling.
- Permission denied. Ask an administrator to grant
VIEW ANY COLUMN ENCRYPTION KEY DEFINITIONandVIEW ANY COLUMN MASTER KEY DEFINITION, or run the query as a user withdb_ownerrights. - GUID does not match documentation. Confirm you are on the same database and server as the documentation. Restores and copies can carry different GUIDs if keys were recreated.
- Function appears to “not exist”.
key_guidis a column in catalog views, not a standalone scalar function. Do not call it asSELECT key_guid();.
Frequently Asked Questions
Is key_guid a function or a column?
It is a column exposed by the Always Encrypted catalog views sys.column_encryption_keys and sys.column_master_keys. You select it; you do not call it like a scalar function.
Can I use key_guid to create a new encryption key?
No. It only reports the GUID of an existing key. Create keys with CREATE COLUMN MASTER KEY and CREATE COLUMN ENCRYPTION KEY.
Does key_guid reveal the actual encryption key?
No. It returns only the identifier. The key material stays in the key store (certificate store, Azure Key Vault, or a hardware security module).
Why does the query return no rows?
Either the database has no Always Encrypted keys, or you are connected to the wrong database. Check the database context and re-run the key listing queries.
Which SQL Server versions support it?
SQL Server 2016 and later, plus Azure SQL Database. Earlier versions do not include Always Encrypted catalog views.
Can I filter encrypted columns by key_guid?
Yes. Join sys.column_encryption_keys to sys.columns through column_encryption_key_id and filter on key_guid to find columns protected by a specific key.