SQL Server OBJECT_ID_FROM_EDGE_ID: How to Use It
In this article
SQL Server's OBJECT_ID_FROM_EDGE_ID function returns the object ID of the graph edge table that contains a given edge ID. It is the companion to EDGE_ID_FROM_OBJECT_ID and is used almost exclusively with graph tables: you have an edge_id value (for example, from an $edge_id pseudo-column or a saved query result) and you need to know which edge table it belongs to, or you need to join back to the underlying table. The function is available only in SQL Server 2017 (14.x) and later, and only in databases that support graph features. You call it as OBJECT_ID_FROM_EDGE_ID ( edge_id ) and it returns an int object ID, or NULL if the edge_id is not valid.
What You'll Need
- SQL Server 2017 or later, or Azure SQL Database / Azure SQL Managed Instance with graph support.
- A database containing at least one node table and one edge table created with
AS NODEandAS EDGE. - Permission to run
SELECTagainst the graph tables (the function itself does not require special permissions beyond access to the object). - A query window in SQL Server Management Studio (SSMS), Azure Data Studio, or any client that can run T-SQL.
Step-by-Step
- Confirm your SQL Server version. Run
SELECT @@VERSION;orSELECT SERVERPROPERTY('ProductMajorVersion');. Graph functions such asOBJECT_ID_FROM_EDGE_IDrequire SQL Server 2017 or newer. On older versions the function does not exist and you will get an "Invalid object name" or "not a recognized built-in function" error. - Create or identify an edge table. If you do not already have one, create a simple graph schema. For example:
CREATE TABLE Person AS NODE;andCREATE TABLE Knows AS EDGE;. Edge tables automatically include hidden columns such as$edge_id,$from_id, and$to_id. - Insert a row into the edge table so you have an edge_id to work with. For example:
INSERT INTO Person (name) VALUES ('Alice'), ('Bob');thenINSERT INTO Knows ($from_id, $to_id) SELECT p1.$node_id, p2.$node_id FROM Person p1, Person p2 WHERE p1.name = 'Alice' AND p2.name = 'Bob';. The exact insert syntax depends on your schema; the key point is that the edge table now has at least one row. - Retrieve an edge_id. Run
SELECT $edge_id FROM Knows;. The$edge_idpseudo-column returns a binary(8) or varbinary value that uniquely identifies the edge row. Copy one value, or keep the query as a subquery for the next step. - Call the function. Use
SELECT OBJECT_ID_FROM_EDGE_ID($edge_id) FROM Knows;or pass a literal/variable:DECLARE @e varbinary(8) = 0x...; SELECT OBJECT_ID_FROM_EDGE_ID(@e);. The return value is the object_id of the edge table (for example, the object_id ofKnows). - Verify the result. Compare the returned value with
SELECT OBJECT_ID('Knows');. They should match. You can also join the result tosys.objects:SELECT o.name FROM sys.objects o WHERE o.object_id = OBJECT_ID_FROM_EDGE_ID(@e);. - Use it in a join. A common pattern is to resolve which edge table an edge_id came from when you have stored edge IDs in an audit or log table. Join the log table to
sys.objectson the function result to get the table name, then branch your logic or build dynamic SQL.
Common Mistakes
Passing a node ID instead of an edge ID. OBJECT_ID_FROM_EDGE_ID expects an edge_id. If you pass a $node_id value, the function returns NULL or an unrelated object ID. Use OBJECT_ID_FROM_NODE_ID for nodes.
Assuming the function validates existence. The function maps an edge_id to an object ID based on the internal graph metadata. If the edge row has been deleted, the mapping may still resolve or may return NULL depending on the situation; do not rely on it as an existence check. Query the edge table directly for that.
Using it on non-graph tables. The function is designed for edge tables. Passing an arbitrary varbinary value will not give meaningful results.
Running on an older compatibility level or edition. Graph features require SQL Server 2017 or later. If you restore a database to an older instance, graph tables and these functions are unavailable.
Forgetting the schema context. The function returns an object_id, which is unique within a database but not across databases. If you work with multiple databases, include the database name in your lookup against sys.objects.
Tips
Use OBJECT_ID_FROM_EDGE_ID together with EDGE_ID_FROM_OBJECT_ID when you need to round-trip between an edge row and its table. The pair is useful for building generic graph utilities, such as a stored procedure that accepts an edge_id and returns the table name and row.
Store edge IDs as varbinary(8) in your log or audit tables to avoid implicit conversions. Mixing binary and varbinary can cause index scans instead of seeks.
When debugging, always select the raw $edge_id alongside the function result so you can see exactly which value produced which object ID.
Remember that object IDs can change after a database rebuild, restore to a new database, or drop/recreate of the table. Do not persist object IDs as permanent references; persist the table name or resolve the ID at query time.
Troubleshooting
Error: 'OBJECT_ID_FROM_EDGE_ID' is not a recognized built-in function name. Your SQL Server version is older than 2017, or you are connected to a database that does not support graph features. Check @@VERSION and the database compatibility level.
The function returns NULL. The edge_id value is not a valid edge identifier, is NULL, or belongs to a different database. Confirm the value comes from an edge table's $edge_id column and that you are in the correct database context.
The returned object ID does not match the table you expected. You may have passed a node_id, or the edge_id belongs to a different edge table. Query sys.objects with the returned ID to see the actual table name.
Permission errors. If you can run the function but cannot see the table name in sys.objects, you may lack VIEW DEFINITION permission. Ask an administrator to grant it, or query sys.tables with appropriate permissions.
Performance issues in large joins. Calling the function per row in a large join can be slow. Consider resolving the object ID once per distinct edge_id value, or persist the table name alongside the edge_id when you write the log.
Frequently Asked Questions
What does OBJECT_ID_FROM_EDGE_ID return?
It returns the int object ID of the edge table that owns the supplied edge_id, or NULL if the value is not a valid edge identifier.
Is there a matching function for node tables?
Yes. OBJECT_ID_FROM_NODE_ID performs the same lookup for node IDs, and NODE_ID_FROM_OBJECT_ID / EDGE_ID_FROM_OBJECT_ID go the other direction.
Can I use it on SQL Server 2016?
No. Graph tables and the associated ID functions were introduced in SQL Server 2017. On 2016 and earlier the function does not exist.
Does the function work across databases?
The function resolves within the current database context. Object IDs are database-scoped, so run it in the database that contains the edge table, or qualify your queries accordingly.
Can I store the returned object ID permanently?
It is not recommended. Object IDs can change after restore, rebuild, or drop/recreate operations. Store the edge_id or the table name instead and resolve at query time.
Why does the function return NULL for a value I copied from $edge_id?
Check that you copied the full binary value without truncation, that the row still exists, and that you are connected to the correct database. A truncated or converted value will not match.