Mysql – Crystal Reports XI and MySQL Stored Procedure with Parameters

crystal-reportsMySQLreportstored-procedures

I am having a problem with a Crystal Report that displays data from a MySQL table. I am currently gathering the data directly from the table, however when the users try to input parameters, problems arise such as:

  1. null values for parameters returning errors
  2. parameters not working as specified

I then created a stored procedure to return data if a parameter is empty and will make the MySQL server do the work rather than the Crystal Reports server.

However Crystal Reports doesn't appear to recognize this and I am having some trouble displaying the results of the procedure.

Here is a copy of the procedure i am using:

Create Procedure sp_report
(IN @param1 varchar(64),
 IN @param2 varchar(64),
 IN @param3 int )

Begin

IF @param1 is null AND @param2 is null AND @param3 is null Then
  Select * from tblData
ELSE IF @param1 is null AND @param2 is not null AND @param3 is not null then 
  Select * from tblData where field3 = @param3 and field2 = @param2
ELSE IF @param1 is not null AND @param2 is not null AND @param3 is  null then 
  Select * from tblData where field2 = @param2 and field1 = @param1
ELSE IF @param1 is not null AND @param2 is null AND @param3 is not null then 
  Select * from tblData where field3 = @param3  and field1 = @param1 
ELSE IF @param1 is not null AND @param2 is null AND @param3 is null then 
  Select * from tblData where field1 = @param1
ELSE IF @param1 is  null AND @param2 is not null AND @param3 is  null then 
  Select * from tblData where  field2 = @param2
ELSE IF @param1 is null AND @param2 is null AND @param3 is not null then
  Select * from tblData where field3 = @param3
ELSE IF @param1 is not null AND @param2 is not null AND @param3 is not null then 
  Select * from tblData where field3 = @param3 and field2 = @param2 and field1 = @param1
END;

Is there an easier way to do this or am I doing something wrong? Any suggestions would be greatly appreciated.

Best Answer

If I'm reading your IF tree correctly, I think you could do this instead (I'm a T-SQL guy, so I can't confirm if this will run in MySQL):

SELECT *
  FROM tblData
 WHERE ((field1=@param1) OR (@param1 is null))
   AND ((field2=@param2) OR (@param2 is null))
   AND ((field3=@param3) OR (@param3 is null))
Related Topic