-1

Basically i have a Stored Procedure which accepts 8 parameters, and based on all these parameters only it's executing the query and giving me an output, but now i have a situation where i can either pass a single value or multiple value anywhere in between 1 to 8 parameters, it should work flawlessly and it should return some value based on the parameters that has been passed. My StoredProcedure is in MySQL, and i'm calling this through CallableStatement in Java code. Below is the snippet of my Storedprocedure.

DELIMITER $$


CREATE PROCEDURE get_details(IN cus_name varchar(255),u_id int, ent_type varchar(255)
,branchId varchar(255), from_date datetime,to_date datetime, val varchar(255),rep_status varchar(255)
)
BEGIN
SELECT
r.id,r.parent_request_id,r.customer_master_id,
r.user_master_id,r.request_status,

cm.customer_name,

u.unique_id,

c.branch_id,c.created_date,c.entity_name,
c.entity_status,c.entity_type,c.no_of_documents,
c.no_of_parties,c.request_id,c.validity,c.itr_validity

FROM request as r
left join user_master as u on r.user_master_id = u.id
left join customer_master as cm  on u.customer_master_id = cm.id
left join customer_entity as c  on r.id= c.request_id

where 
customer_name = cus_name and 
unique_id = u_id and
entity_type = ent_type and
branch_id = branchId and
created_date >= from_date and
created_date <= to_date and
validity = val and 
request_status = rep_status and
entity_status='parent';

END$$
DELIMITER ;

and Below is my code to call this StoredProcedure in Java

String query = "{ call get_details(?,?,?,?,?,?,?,?)}";
            ResultSet resultSet = null;
            List<SuperAdmin> admins = new ArrayList<SuperAdmin>();
            try(
                    Connection connection = getconnection();
                    CallableStatement callableStatement = connection.prepareCall(query)){
                if(ReportDTO.getCustomerName() != null){
                callableStatement.setString(1, ReportDTO.getCustomerName());
                }
                if(ReportDTO.getUniqueId() != null){
                callableStatement.setString(2, ReportDTO.getUniqueId());
                }
                if(ReportDTO.getEntity_type() != null){
                callableStatement.setString(3, ReportDTO.getEntity_type());
                }
                if(ReportDTO.getBranchId() != null){
                callableStatement.setString(4, ReportDTO.getBranchId().toString());
                }
                if(ReportDTO.getFilter_from() != null){
                    Date newFilterDate_from =  dateFormat.parse(ReportDTO.getFilter_from().toString());
                    String newFormattedDate = dateFormat2.format(newFilterDate_from);
                    callableStatement.setString(5, newFormattedDate);
                }
                if(ReportDTO.getFilter_to() != null){
                    Date newFilterDate_to = (Date) dateFormat.parse(ReportDTO.getFilter_to().toString());
                    String newFormattedDateTo = dateFormat2.format(newFilterDate_to);
                    callableStatement.setString(6,newFormattedDateTo);
                }
                if(ReportDTO.getValidity() != null){
                callableStatement.setString(7, ReportDTO.getValidity());
                }
                if(ReportDTO.getReport_status() != null){
                callableStatement.setString(8, ReportDTO.getReport_status());
                }
                resultSet = callableStatement.executeQuery();
                while(resultSet.next()){
                    SuperAdmin sAdmin = new SuperAdmin();
                    sAdmin.setRequest_status(resultSet.getString("request_status"));
                    sAdmin.setCustomer_entity_type(resultSet.getString("entity_type"));
                    sAdmin.setCustomer_entity_name(resultSet.getString("entity_name"));
                    sAdmin.setRequest_id(resultSet.getInt("id"));
                    sAdmin.setCustomer_entity_created_date(resultSet.getDate("created_date"));
                    sAdmin.setCustomer_entity_status(resultSet.getString("entity_status"));
                    sAdmin.setCustomer_entity_no_of_documents(resultSet.getString("no_of_documents"));
                    sAdmin.setCustomer_entity_no_of_parties(resultSet.getString("no_of_parties"));
                    sAdmin.setCustomer_entity_validity(resultSet.getString("validity"));
                    sAdmin.setCustomer_entity_itr_validity(resultSet.getString("itr_validity"));
                    sAdmin.setCustomer_entity_branch_id(resultSet.getLong("branch_id"));
                    admins.add(sAdmin);
                }

            }

any help would be highly appreciable.Thanks well in advance.

Mavericks
  • 283
  • 1
  • 7
  • 20
  • did you get any error with this or what exactly you want to do? – Youcef LAIDANI Feb 28 '17 at 09:04
  • stored procedures do not support variable number of arguments – e4c5 Feb 28 '17 at 09:06
  • @YCF_L yes i did get java.sql.SQLException: No value specified for parameter 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920) – Mavericks Feb 28 '17 at 09:06
  • you can edit your question, and you can put your error there – Youcef LAIDANI Feb 28 '17 at 09:07
  • @e4c5 can you share across a few of the Example – Mavericks Feb 28 '17 at 09:08
  • maverick I said it isn't supported, so how can I share examples for something that's not there? – e4c5 Feb 28 '17 at 09:10
  • @e4c5 http://stackoverflow.com/questions/1810638/optional-parameters-in-sql-server-stored-proc You can refer to this link, where they are passing a few parameters – Mavericks Feb 28 '17 at 09:12
  • First of all that question is not mysql. Second of all they are setting parameters to default of null – e4c5 Feb 28 '17 at 09:14

1 Answers1

0

For optional parameter in stored procedure you can write like this

CREATE PROCEDURE get_details(IN cus_name varchar(255)='',u_id int=0, ent_type varchar(255)='',
branchId varchar(255)='', from_date datetime=null,to_date datetime=null, val    varchar(255)='',rep_status varchar(255)=''
 )
BEGIN
you query syntex
Amit Yadav
  • 481
  • 3
  • 11