What TypeScript Type Should I Use for Multiple SQL Server Rows?

September 15, 2026 3 answers
Asked by temporary fix
I'm building an Express app with TypeScript and using the mssql package to communicate with SQL Server. One of my stored procedures returns multiple rows, with three related fields per row. I need to store the result in a structure like an array of objects, for example [{ field1: value1, field2: value2, field3: value3 }, ...]. I initially tried using Map and string[], but a map entry for each field loses the relationship between fields in the same row. What type and structure should I use to hold the result? Is there also a recommended way to return these stored procedure results to a Flutter app?

Answers

regular expression+
The mssql result normally exposes the returned rows through `result.recordset`. If your stored procedure returns products, for example, you could write: `interface Product { productId: number; productName: string; } const products: Product[] = result.recordset;`. Each element represents one complete row, keeping all related columns together.
Reply to this answer
ast_node9
Define a TypeScript type or interface representing one row, then create an array of that type. For example: `type Item = { field1: string; field2: string; field3: string; }; const returnList: Item[] = []; returnList.push({ field1: row.field1Value, field2: row.field2Value, field3: row.field3Value });`. You can then access a value with something like `returnList[1].field2`.

temporary fix: That makes sense. I wasn't sure whether the result was already an array of objects, so I'll inspect the variable and use this structure if needed.

pita.pocket: You can check the shape by logging `JSON.stringify(variableSourceOfRow)`. If the source is already an array, it may already have exactly the structure you need and you won't need to rebuild it.

Reply to this answer
railway points!!
If you don't want to define a specific interface yet, you can use `const rows: Record[] = result.recordset;` and iterate with `for (const row of rows)`. A named interface is usually preferable because it gives you better type checking and editor support.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions