SQL placeholder in WHERE IN, inserted rows do not work

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 , ( ), .

? , ?

+3
3

, .

select *
from pitotal
where tag IN (SELECT tag from pipoint WHERE INSTR(?, tag) <> 0) and time between 'y' and 't'

.

, -

+1

, , , . , , "", .

, , ( , , ), SQL . (, - SQL-).

+1

, , 5 . NULL, " " ( NULL, , , ).

( ):

WHERE tag IN (?,?,?,?,?)

, , : NULL, .

, 7,3,1,8,9, :

WHERE tag IN (7,3,1,8,9)

7,NULL,1,NULL,9, :

WHERE tag IN (7,7,1,7,9)

.. , IN , . .

kludge , IN, .

+1

Source: https://habr.com/ru/post/1736939/


All Articles