The KEY_ID function in SQL Server returns the integer ID of a symmetric or asymmetric key, or of a certificate, in the current database. You use it to look up the internal identifier that SQL Server assigns to a key object, which is useful when you are scripting permissions, auditing encryption objects, or joining system catalog views. The function takes one argument, the key name, and returns an int. If the key does not exist, it returns NULL. You need a database that already contains a key or certificate, and you need at least VIEW DEFINITION permission on that object, or membership in a role that has it.
What You'll Need
- A SQL Server instance (any edition that supports encryption, which includes Express, Standard, and Enterprise).
- A database where you have permission to query system catalog views or run SELECT statements.
- At least one symmetric key, asymmetric key, or certificate already created in that database. If none exist, you can create a test one with CREATE SYMMETRIC KEY.
- VIEW DEFINITION permission on the key object, or a role such as db_owner or sysadmin.
- A query tool such as SQL Server Management Studio (SSMS), Azure Data Studio, or sqlcmd.
Step-by-Step
- Connect to the correct database. KEY_ID works in the context of the current database. In SSMS, use the database dropdown in the toolbar, or run
USE YourDatabaseName;at the top of your query window. If you run KEY_ID in master, it will only find keys in master, not in your application database. - Confirm the key exists. Run a query against the catalog view that matches the key type. For symmetric keys:
SELECT name FROM sys.symmetric_keys;. For asymmetric keys:SELECT name FROM sys.asymmetric_keys;. For certificates:SELECT name FROM sys.certificates;. The name you see is the argument you will pass to KEY_ID. - Call KEY_ID with the key name. The syntax is
KEY_ID ( 'key_name' ). For example:SELECT KEY_ID('MySymmetricKey') AS KeyId;. The result is an integer such as 256 or 257. That number is the internal ID SQL Server uses in catalog views like sys.key_encryptions and sys.crypt_properties. - Use the result in a join or filter. A common pattern is to join sys.symmetric_keys to sys.key_encryptions on the key ID. For example:
SELECT k.name, e.crypt_type FROM sys.symmetric_keys k JOIN sys.key_encryptions e ON k.symmetric_key_id = e.key_id WHERE k.symmetric_key_id = KEY_ID('MySymmetricKey');. This shows which certificates or keys protect your symmetric key. - Check for NULL. If KEY_ID returns NULL, the name you passed does not match any key or certificate visible to you in the current database. Double-check spelling, case sensitivity (if the database uses a case-sensitive collation), and that you are in the right database. Also confirm you have VIEW DEFINITION permission; without it, the object may exist but be hidden from your session.
- Test with a certificate or asymmetric key. KEY_ID works the same way for certificates:
SELECT KEY_ID('MyCertificate');. For asymmetric keys:SELECT KEY_ID('MyAsymmetricKey');. The function does not distinguish between object types, so if a symmetric key and a certificate share the same name, KEY_ID returns the ID of whichever object it finds first; avoid duplicate names across key types. - Record or script the result. If you are building a deployment script, capture the ID in a variable:
DECLARE @kid int = KEY_ID('MySymmetricKey');then use @kid in subsequent statements. This keeps your script readable and avoids repeating the function call.
Common Mistakes
- Running KEY_ID in the wrong database. The function is database-scoped. A key that exists in your application database will return NULL if you call KEY_ID while connected to master.
- Assuming KEY_ID creates a key. It only looks up an existing object. If you need a new key, use CREATE SYMMETRIC KEY, CREATE ASYMMETRIC KEY, or CREATE CERTIFICATE.
- Passing a variable without quotes. The argument is a string literal or a variable of a string type.
KEY_ID(MyKey)without quotes will be interpreted as a column name and fail. - Expecting KEY_ID to work across databases. There is no three-part name form. To find a key in another database, switch context with USE or query that database's catalog views directly.
- Confusing KEY_ID with key_guid. KEY_ID returns an integer; the key_guid column in catalog views returns a uniqueidentifier. They are different values and are not interchangeable.
- Ignoring permissions. A user with only SELECT on user tables may not see metadata for keys. If KEY_ID returns NULL for a key you know exists, check VIEW DEFINITION.
Tips
- Use KEY_ID inside a SELECT that also pulls the key name from the catalog view, so you can verify the mapping in one result set.
- When auditing encryption, join sys.key_encryptions to sys.symmetric_keys and sys.certificates using the ID columns, not the names, because names can be duplicated across object types.
- Wrap KEY_ID in ISNULL or a CASE expression in reports so NULL values are labeled clearly rather than appearing as blank cells.
- If you are scripting permissions with GRANT or DENY, you can use the key name directly; KEY_ID is mainly for catalog queries and dynamic SQL where you need the numeric ID.
- Keep a comment in your scripts explaining why a hard-coded key name is used, since key names are case-sensitive under some collations.
Troubleshooting
If KEY_ID returns NULL, work through these checks in order. First, confirm the database context with SELECT DB_NAME();. Second, list the objects of the relevant type from the catalog views and compare the exact spelling. Third, test with a key you know exists, such as one created in a test database, to confirm the function itself works for your login. Fourth, check permissions: run SELECT HAS_PERMS_BY_NAME('MySymmetricKey', 'OBJECT', 'VIEW DEFINITION'); and see whether it returns 1. If it returns 0, ask an administrator to grant VIEW DEFINITION on the key. Fifth, if you are using contained databases or Azure SQL, confirm the key was created in the user database and not in master. If the function returns an error rather than NULL, the most common cause is a syntax problem such as missing quotes or an extra argument; KEY_ID accepts exactly one argument.
If you cannot see the key in the catalog view at all, the object may have been dropped, or your login may lack metadata visibility. In that case, connect with a sysadmin account to verify, then grant the minimum permission needed to the original login rather than leaving elevated access in place.
Frequently Asked Questions
What does KEY_ID return for a key that does not exist?
It returns NULL. It does not raise an error, so always check for NULL in your script logic.
Does KEY_ID work for certificates and asymmetric keys?
Yes. The function returns the ID for symmetric keys, asymmetric keys, and certificates in the current database.
Can I use KEY_ID across databases?
No. It is scoped to the current database. Switch to the target database with USE, or query that database's catalog views from a stored procedure that runs in that context.
Is KEY_ID the same as key_guid?
No. KEY_ID returns an integer identifier; key_guid returns a uniqueidentifier. Use the one that matches the column you are joining to.
What permission do I need to run KEY_ID?
You need VIEW DEFINITION on the key object, or membership in a role that has it, such as db_owner or sysadmin. Without it, the function may return NULL even when the key exists.
Can KEY_ID be used in a WHERE clause?
Yes, but it is usually clearer to compare the catalog view's ID column to KEY_ID('name') in a join or filter, as shown in the steps above.