0

lets say I have table issues with following data:

+------+------------+
| id   | project_id |
+------+------------+
|  101 |          2 |
|  102 |          2 |
|  103 |          2 |
|  104 |          2 |
|  105 |          2 |
|  106 |          2 |
|  107 |          2 |
|  123 |          9 |
|  234 |         63 |
|  454 |         77 |
|  893 |         73 |
| 1233 |        101 |
+------+------------+

And for example I want to count number of issues in projects with project_id 2,64 and 77. I can do that with following SQL statement

select count(*) from issues where project_id in (2,64,77);

But what if I didn't want to create this statement every time but save it somehow and just pass the ids of projects i need count for? So I would need something like following function which I can call like

select my_function(2,64,77);

But the problem is it seems like I can't create function with such dynamic parameters. Is there any way to do that? Of course it doesn't have to be function - I can use view, procedure, function or anything else as long as I can save my sql statement and just pass ids as parameter. I'm also fine with passing SQL that produces the ids I need so something like

select my_function("select id from projects where name like '%qwerty%'")

is also fine.

Of course in real word this statement is much harder than this example and writing it each time is not only hard but can also be error-prone

Rafał Pydyniak
  • 539
  • 4
  • 11
  • 'writing it each time is not only hard but can also be error-prone' - and passing parameters is not? – P.Salmon Mar 16 '18 at 09:56
  • Well when I pass parameter I can only make mistake in the ids I pass to a method. If however I write full sql statement (with multiple where, group by and joins) then I guess there is a lot more place for mistake :) – Rafał Pydyniak Mar 16 '18 at 11:12

0 Answers0