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?
3 Answers
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.
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`.
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.
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.

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.