The KEY_NAME function in SQL Server returns the name of a symmetric key or asymmetric key when you supply its unique identifier (GUID). It is most often used to turn the cryptic key_guid values in system views like sys.symmetric_keys and sys.asymmetric_keys into readable names, or to verify that a key exists before you try to use it. You can run it in any database where you have permission to view the key metadata, and no special setup is required beyond a SQL Server instance and a query window such as SQL Server Management Studio (SSMS) or Azure Data Studio.

What You'll Need

  • A connection to a SQL Server instance (any edition that supports encryption, which includes Express, Standard, Enterprise, and Azure SQL Database).
  • A query tool such as SSMS or Azure Data Studio, or any client that can run T-SQL.
  • Permission to view the key metadata in the target database. Membership in the db_owner role or CONTROL permission on the database is sufficient; otherwise you may see NULL or an error.
  • At least one symmetric or asymmetric key already created in the database if you want to see a non-NULL result. You can still test the function with a made-up GUID to see how it behaves.

Step-by-Step

  1. Connect to the correct database. KEY_NAME works in the context of the current database. In SSMS, use the database dropdown in the toolbar or run USE YourDatabaseName; before calling the function. If you run it in master, you will only see keys that exist in master.
  2. Find a key GUID to test with. Query the system catalog view for symmetric keys:
    SELECT name, key_guid FROM sys.symmetric_keys;
    For asymmetric keys, use:
    SELECT name, key_guid FROM sys.asymmetric_keys;
    Copy one of the key_guid values. The GUID is a uniqueidentifier data type.
  3. Call KEY_NAME with that GUID. The basic syntax is:
    SELECT KEY_NAME('00000000-0000-0000-0000-000000000000');
    Replace the placeholder with the GUID you copied. The function returns a sysname (nvarchar(128)) containing the key name, or NULL if no key with that GUID exists in the current database.
  4. Use it in a join or subquery for readable output. A common pattern is to combine KEY_NAME with the catalog view so you can see both the GUID and the name side by side:
    SELECT name, key_guid, KEY_NAME(key_guid) AS resolved_name
    FROM sys.symmetric_keys;
    This is handy when you are auditing which keys exist and want to confirm that the function resolves each GUID correctly.
  5. Check for NULL results. If KEY_NAME returns NULL, either the GUID does not match any key in the current database, or you lack permission to see that key. Switch to the database where the key was created and verify your permissions. Note that KEY_NAME does not search across databases; a key in another database will not be found.
  6. Test with an invalid GUID. Run SELECT KEY_NAME(NEWID()); to see the NULL behavior. NEWID() generates a random GUID that almost certainly does not exist, so the result will be NULL. This confirms the function is working and simply not finding a match.
  7. Use the result in dynamic SQL if needed. Because KEY_NAME returns a name, you can build statements like OPEN SYMMETRIC KEY ... by concatenating the result. For example:
    DECLARE @keyName sysname = KEY_NAME('your-guid-here');
    IF @keyName IS NOT NULL
        EXEC('OPEN SYMMETRIC KEY ' + QUOTENAME(@keyName) + ' DECRYPTION BY CERTIFICATE YourCert;');
    Always use QUOTENAME to avoid injection and syntax errors with unusual key names.

Common Mistakes

  • Running in the wrong database. KEY_NAME only looks at the current database. If you run it in master but the key lives in SalesDB, you get NULL. Always set the database context first.
  • Passing a name instead of a GUID. The function expects a uniqueidentifier. Passing a string like 'MyKey' will cause an error or an implicit conversion failure. Use the key_guid column, not the name column.
  • Assuming NULL means the key is missing. NULL can also mean you lack permission. Check whether you can see the key in sys.symmetric_keys or sys.asymmetric_keys; if the row is hidden, KEY_NAME will return NULL even though the key exists.
  • Forgetting that KEY_NAME is not a lookup across servers. It does not query linked servers or other instances. You must run it on the instance and database where the key was created.
  • Using it on a database where encryption is not set up. If no keys exist, the function returns NULL for every GUID. That is expected, not a bug.

Tips

  • Use KEY_NAME in audit queries to replace raw GUIDs with human-readable names in reports.
  • Combine it with sys.key_encryptions to see which keys are protected by which certificates or other keys.
  • Remember that KEY_NAME is a metadata function; it does not decrypt or open anything. It only resolves a GUID to a name.
  • If you need to resolve a GUID for a key in another database, write a query against that database's catalog views instead of relying on KEY_NAME from the current context.
  • When scripting key management, always check for NULL before using the result in dynamic SQL to avoid errors like 'Cannot open key NULL'.

Troubleshooting

If KEY_NAME returns NULL and you are sure the GUID is correct, first confirm you are in the right database with SELECT DB_NAME();. Then check that the key appears in the catalog view: SELECT * FROM sys.symmetric_keys WHERE key_guid = 'your-guid';. If the row is missing, the key may have been dropped or you may lack permission. If the row exists but KEY_NAME still returns NULL, verify your permissions by trying as a sysadmin or db_owner. If you get an error like 'Invalid object name' or 'Incorrect syntax', check that you are using the function correctly: KEY_NAME ( 'guid' ) with a valid uniqueidentifier string. If you pass a variable, ensure it is of type uniqueidentifier. For permission errors, ask an administrator to grant VIEW DEFINITION on the key or database. If the function works but returns an unexpected name, remember that key names are not guaranteed unique across databases, only within a database.

Frequently Asked Questions

What does KEY_NAME return if the key does not exist?

It returns NULL. No error is raised for a missing key; you simply get NULL.

Can I use KEY_NAME with asymmetric keys?

Yes. It works with both symmetric and asymmetric keys, as long as you pass the key_guid from the corresponding catalog view.

Does KEY_NAME work in Azure SQL Database?

Yes, it is supported in Azure SQL Database and Azure SQL Managed Instance, provided the database contains keys and you have the necessary permissions.

Is KEY_NAME case-sensitive?

The function name is not case-sensitive in most collations, but the GUID comparison is exact. Use the GUID exactly as stored.

Can I call KEY_NAME from a stored procedure?

Yes, it is a scalar function and can be used anywhere a scalar expression is allowed, including stored procedures, functions, and views.

Why does KEY_NAME return NULL for a key I can see in the GUI?

The GUI may be connected to a different database or instance. Confirm your query window's database context matches where the key was created.