Brian Var
Brian Var

Reputation: 6227

How to pass an array into mssql query string?

I'm using the mssql node package to perform queries on a SQL Server DB. Now I want to pass in an array of intergers to the query string. So I grab The array from req.query.rids and the values output are as follows:

var RIDS = JSON.parse(req.query.rids);
logger.info(`${RIDS}`)  // result shows: 204294,204303,104354

But when I use the IN operator to check for a match to any of the array values. I get the error error: Incorrect syntax near '204294'. What I understand from this is that the RID array being passed in is not in the correct format for the query.

Question:

How do I pass in an array into mssql query string? `

This is a snippet of the query where I check for a match in the RIDS array:

Left Join [Metrics_DB].[dbo].[AssetBundle]
  on [Metrics_DB].[dbo].[Assetdata].RID=[Metrics_DB].[dbo].[AssetBundle].[RID]
   where [Asset Sunset Date] is not null and [OSM_Metrics_DB].[dbo].[Assetdata].RID IN ${RIDS}

Upvotes: 0

Views: 245

Answers (1)

Mox
Mox

Reputation: 598

You need to stringify and format your array in the style of (item, item, item).

Source

Upvotes: 1

Related Questions