I'm encountering an error when executing a paginated SQL query in my application. The query is designed to fetch a set of records from a database with a FETCH NEXT clause for pagination. However, upon execution, it returns a 500 error with the message "Invalid usage of the option NEXT in the FETCH statement."
Here is the relevant portion of the query that seems to be causing the issue:
let pipelineCondition = "";
if (zodValidate.data?.pipeline_id) {
pipelineCondition = `AND dl.pipeline = '${zodValidate.data.pipeline_id}'`;
}
const dealPipeline = await sql.query`
SELECT
dl.DealId AS deal_id,
amount,
amount_in_home_currency,
closed_lost_reason,
closed_won_reason,
dl.closedate AS closed_date,
dl.days_to_close,
deal_currency_code,
dealname AS deal_name,
c.CompanyName AS company_name,
dps.StageLabel AS deal_stage_label,
dealtype AS deal_type,
service,
service_term,
dl.contarct_lenght as contract_length,
commencement_date,
dl.hs_all_owner_ids,
o.hubspot_owner_id,
o.email AS owner_email,
hs_is_closed,
hs_is_closed_won,
dl.hs_lastmodifieddate AS hs_last_modified_date,
dl.pipeline AS pipeline_id,
dp.PipelineLabel AS deal_pipeline_label,
dl.createdate AS created_date
FROM Deal dl
INNER JOIN DealPipelineStage dps ON dps.DealStage = dl.dealstage
INNER JOIN DealPipeline dp ON dp.PipelineId = dl.pipeline
INNER JOIN DealCompanyAssociations dcs ON dcs.DealId = dl.DealId
INNER JOIN Company c ON c.companyId = dcs.companyId
INNER JOIN Owner o ON o.hubspot_owner_id = dl.hubspot_owner_id
WHERE (YEAR(dl.createdate) = ${currentYear} OR YEAR(dl.createdate) = ${previousYear})
${pipelineCondition}
ORDER BY dl.createdate DESC
OFFSET ${offset} ROWS
FETCH NEXT ${pageSize} ROWS ONLY;
`;
Response I get
{
"success": false,
"statusCode": 500,
"data": null,
"message": "Invalid usage of the option NEXT in the FETCH statement."
}
I'm encountering an error when executing a paginated SQL query in my application. The query is designed to fetch a set of records from a database with a
FETCH NEXT
clause for pagination. However, upon execution, it returns a 500 error with the message "Invalid usage of the option NEXT in the FETCH statement."Here is the relevant portion of the query that seems to be causing the issue:
Response I get