Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server EDGE_ID_FROM_PARTS: How to Use It

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

EDGE_ID_FROM_PARTS is a built-in SQL Server function that builds a single bigint edge ID from two parts: the object ID of the graph edge table and a unique integer within that table. It exists so you can address an edge directly in graph queries without scanning for its hidden internal columns. You need SQL Server 2017 or later with graph database support enabled, and the function only makes sense on a table that was created with AS EDGE.

What You'll Need

  • SQL Server 2017 or newer (any edition that supports graph tables), or Azure SQL Database with graph support.
  • An existing edge table created with CREATE TABLE ... AS EDGE.
  • Permission to run SELECT queries against that table.
  • A client such as SQL Server Management Studio, Azure Data Studio, or sqlcmd.

Step-by-Step

  1. Confirm the table is an edge table. Run SELECT name, is_edge FROM sys.tables WHERE name = 'YourEdgeTable';. The is_edge column should be 1. If it is 0, the table is a node table or a regular table and EDGE_ID_FROM_PARTS has nothing valid to build from.
  2. Find the object ID of the edge table. Use SELECT OBJECT_ID('dbo.YourEdgeTable') AS object_id;. This returns the integer that forms the first part of the edge ID. It is stable for the life of the table in that database.
  3. Find the integer that forms the second part. Edge tables have a hidden column called $edge_id. Query it with SELECT $edge_id FROM dbo.YourEdgeTable;. The value is a bigint that already encodes both parts, so you do not normally need to assemble it yourself.
  4. Call the function when you need to construct an ID. The syntax is EDGE_ID_FROM_PARTS(object_id, edge_id). For example: SELECT EDGE_ID_FROM_PARTS(OBJECT_ID('dbo.YourEdgeTable'), 1) AS edge_id;. The second argument is the integer part, not the full bigint.
  5. Use it in a join or a MATCH clause. You can pass the result into a query that filters edges by their ID, or compare it to $edge_id values returned from the table. This is useful when you stored an edge ID elsewhere and need to look the edge back up.
  6. Verify the result round-trips. Compare your constructed value against the actual $edge_id for the same row: SELECT $edge_id, EDGE_ID_FROM_PARTS(OBJECT_ID('dbo.YourEdgeTable'), 1) FROM dbo.YourEdgeTable WHERE $edge_id = EDGE_ID_FROM_PARTS(OBJECT_ID('dbo.YourEdgeTable'), 1);. If no row comes back, the second argument was wrong.
  7. Keep the object ID in a variable if you reuse it. Declare DECLARE @oid INT = OBJECT_ID('dbo.YourEdgeTable'); and pass @oid to the function. This avoids repeated lookups and makes the query easier to read.

Common Mistakes

The most frequent error is passing the full $edge_id bigint as the second argument. EDGE_ID_FROM_PARTS expects the integer part only, so passing the whole bigint produces a different value or an overflow. Another common mistake is calling the function on a node table or a plain table; the function does not validate that the object is an edge table, so it can return a number that has no matching row. People also hard-code the object ID in scripts that run against multiple databases, where the ID will differ. Finally, some users assume the function creates edges or inserts rows; it only computes an ID, it does not modify data.

Tips

Use OBJECT_ID() inline rather than copying a number from a previous result, because object IDs change when a table is dropped and recreated. If you need to map between the integer part and the full edge ID, remember that the full value is a packed bigint and the integer part is the low portion; the function is the supported way to combine them. Avoid storing constructed edge IDs long-term unless you also store the object ID, since a restore or rebuild can change it. For readability, wrap the call in a small view or a computed expression rather than repeating the two arguments throughout a query. Test any script on a copy of the database before running it in production, especially if it writes results back to a table.

Troubleshooting

If the function returns NULL, check that OBJECT_ID() found the table; a NULL object ID usually means the table name or schema is wrong. If you get an arithmetic overflow, the second argument is too large for the integer part, which almost always means you passed the full $edge_id instead of its integer component. If the query returns no rows when you expected a match, confirm the edge table has not been rebuilt since the ID was stored. If the function is not recognised at all, the database compatibility level may be too low or the server version predates graph support; raise the compatibility level only after checking that the rest of the workload supports it, and upgrade the server if it is older than SQL Server 2017. If permissions block the query, ask an administrator for SELECT rights on the edge table. When in doubt, query $edge_id directly and compare values rather than guessing at the parts.

Frequently Asked Questions

What does EDGE_ID_FROM_PARTS actually return?

It returns a bigint that matches the hidden $edge_id value for an edge, built from the table's object ID and an integer part.

Can I use it on a node table?

No. It is intended for edge tables. On other tables it may return a number, but that number will not correspond to a real edge ID.

Why does my result not match the stored $edge_id?

Almost always because the second argument was the full bigint instead of the integer part, or because the object ID changed after the table was recreated.

Is it available in older SQL Server versions?

No. Graph tables and this function were introduced in SQL Server 2017. Older versions do not recognise it.

Does the function insert or change data?

No. It only computes a value. Inserting edges is done with normal INSERT statements against the edge table.

Should I store the constructed ID in another table?

Only if you also store the object ID, because the ID is not portable across databases or after a table rebuild.