I have a 10 tables with the same schema. I’m trying to create an overloaded stored procedure so that I can union a bunch of tables together with simple selects (SELECT * FROM tableX). If each table has 1000 (different) rows, then I want to create a stored procedure where the following would happen:
CALL getRowsByNum(table1); -> 1000 rows
CALL getRowsByNum(table1, table2, table4); -> 3000 rows
…etc.
I got part of the way through writing 10 overloaded procedures that would SELECT * FROM X UNION ALL SELECT * FROM X UNION ALL….. etc, but that’s really madness.
Anyone have a different suggestion? This silly setup is the result of an architectural decision made a while ago.
Thanks!
Not really. You can’t have variable number of parameters or procedures with the same name.
I dont think there is an easy solution to your problem.
EDIT: I’ve thought of an really ugly solution but I’ll leave it to you if you wanna use it. This is untested pseudo code only.
Create a proc where you take a varchar in, long enough to hold the value “table1,table2,table3,…” and so on for as many tables you like to union at most. (could be another identifier of course)
Write all tables in union. Since they are the same just use * to save time and space
list all your tables. At least you don’t have to list the possible permutations and the user of the procedure won’t know how you did it 🙂