Hi All,
Would it be possible to have an SQL get statement within a function? I am currently attempting to create a function to continuously offset and limit 10,000 records from an SQL database however I am having issues in doing so:
The following code is what I am trying to accomplish within the function. There are two SQL statements to complete the task:
sql(
state =>
`SELECT *
FROM postgres.vaccinedoses
ORDER BY patientid_fk
ASC LIMIT 10000; `, { writeSql: true });
fn(state => {
const vaccinationevents = state.data;
console.log("Fetching Vaccination Event Data Limit 10000...")
return {...state, vaccinationevents};
});
sql(
state =>
`SELECT *
FROM postgres.vaccinedoses
ORDER BY patientid_fk
ASC LIMIT 10000 OFFSET 10000 ; `, { writeSql: true });
fn(state => {
const newVaccinationEvents = state.data;
console.log("Fetching Vaccination Event Data Offset 10000...");
// Append new data to existing vaccinationevents array
const updatedVaccinationEvents = [...state.vaccinationevents, ...newVaccinationEvents];
return { ...state, vaccinationevents: updatedVaccinationEvents };
});
Attempt at function:
function fetchDataWithOffset(offset) {
const sqlQuery = `
SELECT *
FROM postgres.vaccinedoses
ORDER BY patientid_fk
ASC LIMIT 10000 OFFSET ${offset};`;
sql(
state => sqlQuery,
{ writeSql: true }
);
fn(state => {
const newVaccinationEvents = state.data;
console.log("Fetching Vaccination Event Data Offset " + offset + "...");
// Append new data to existing vaccinationevents array
const updatedVaccinationEvents = [...state.vaccinationevents, ...newVaccinationEvents];
return { ...state, vaccinationevents: updatedVaccinationEvents };
});
}
// Call the function for each offset
fetchDataWithOffset(1);
fetchDataWithOffset(2);
- This image highlights the error I am receiving
Any assistance is greatly appreciated.