I have 3 tables:
1. student: {id, name, roll}
2. subject: {id, name}
3. marks: {student_id, subject_id, marks}
I have a procedure which returns all the student and their marks (say getAllMarks() ), i want to create another procedure which returns marks of a given roll no (say getRollMarks(int rollno)). So there are two ways i can create the procedure:
1. write the appropriate SQL to get the result in getRollMarks procedure.
2. call getAllMarks in getRollMarks and then apply where clause on the result returned by getAllMarks.
Can anyone suggest pros and cons of above two methods? I have 2 Pros of method 2:
1.SQL queries won't be repeated across procedures.
2.If anything is changing in one table, the change will be done at one place/procedure, not in every procedure which is using that table.
PS: The tables are just for example, actually they are bigger tables with many parameters to query. The question can be simplified as should we write repeated queries in different procedures, or should we use existing procedures with required clause (where, order by, joins etc)?
If you don't want to repeat the query, you should be using a view. Also performance wise it is faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With