As part of my assignments, I need to write SQL queries to connect to our PI database. To generate a request, I need to pass an array of tags (essentially primary keys), but they must be inserted as strings.
Since this will be a modular query and used for multiple tags, a placeholder is used.
The query is based on using the WHERE IN statement where the placeholder is located, as shown below:
SELECT SUM(value * 5/1000) as "Hourly Flow [kL]"
FROM piarchive..pitotal
WHERE tag IN (?)
AND time between ? and ?
AND timestep = '1d'
AND calcbasis = 'Eventweighted'
AND value <> ''
The problem is the format in which tags should be passed as. If I add them directly to the request (for testing), they go in the format (these are example numbers): '000000012','00000032','005050236','4560236'
and the request looks like this:
SELECT SUM(value * 5/1000) as "Hourly Flow [kL]"
FROM piarchive..pitotal
WHERE tag IN ('000000012','00000032','005050236','4560236')
AND time between ? and ?
AND timestep = '1d'
AND calcbasis = 'Eventweighted'
AND value <> ''
... which is working.
, . 1 , ( ), .
? , ?