Please post preformatted code, and not pictures of code. Makes it more difficult to help when we have to retype the script.
Normally I would say write a query that returns all of the results that you want. The database is going to be much better at this than a script, however, I don't know what the data looks like or what the stored procedures you're running are actually doing.
You can use the DatasetBuilder to accomplish this.
from com.inductiveautomation.ignition.common.util import DatasetBuilder
stackId = '3316'
stackDataQuery = 'EXEC dbo.PIR_GetStackData @stackId = ?'
serialDataQuery = 'EXEC dbo.DET_GetSerialData @serial = ?'
stackDataResults = system.db.runPrepQuery(stackDataQuery, [stackId])
builder = DatasetBuilder.newBuilder()
builder.colNames(['column names for your dataset'])
builder.colTypes(['column types for your columns']) #list of column types must be the same length as the headers
for stack in stackDataResults:
for row in system.db.runPrepQuery(serialDataQuery,[stack]):
builder.addRow(row)
print builder.build()