2016-10-13 2 views
0
CREATE DEFINER=`root`@`localhost` PROCEDURE `EventList_SP`(
in employeeId varchar(45), 
in eventTypevalue int, 
in groupIdArray text, 
in searchTermtext text, 
in skillIdArray text, 
in startDate date, 
in endDate date, 
in offsetvalue int, 
out total int 
) 
BEGIN 

      SET @empID = employeeId; 
      SET @Whereclause='where 1=1'; 

    if(groupIdArray is not null) then 
    set @Whereclause=CONCAT(@Whereclause," and FIND_IN_SET(groupId,'",groupIdArray,"')"); 
    end if; 

     if(skillIdArray is not null) then 
    set @Whereclause=CONCAT(@Whereclause," and FIND_IN_SET(groupId,'",skillIdArray,"')"); 
    end if; 
    if(startDate is not null and endDate is not null) then 
     set @Whereclause=CONCAT(@Whereclause," and (scheduledDate between 
    '",startDate,"' and '",endDate,"')"); 
    end if; 
    if(eventTypevalue is not null and eventTypevalue=0) then 
    set @Whereclause=CONCAT(@Whereclause," and scheduledDate<'",curdate(),"'"); 
    end if; 

     if(eventTypevalue is not null and eventTypevalue=1) then 
      set @Whereclause=CONCAT(@Whereclause," and scheduledDate>'",curdate(),"'"); 
     end if; 

     if(searchTermtext is not null) then 

    set @Whereclause=CONCAT(@Whereclause," and searchTerm like'",searchTermtext,"'"); 
    end if; 

set @SQLQuery =CONCAT("select groupId,eventId,scheduleId,description,events,eventType, 
scheduledDate,name,designation,image,skills,duration,status, 
case when scheduledDate < now() and (select count(*) from event_request where 
event_id=eventId and employee_code='TJU_741')>0 then 1 
when scheduledDate < now() and (select count(*) from event_request where 
event_id=eventId and employee_code='TJU_741')=0 then 0 
else '' end as hasRequested ,(SELECT actual_attendance_status_id FROM TJU.event_attendees_mapping where 
scheduleId=event_schedule_id and employee_code='TJU_741') 
as attendingStatus, 
meetingRoom from EventList_View ", @Whereclause ); 

/* set @SQLQuery=CONCAT("select count(*) from EventList_View",@Whereclause,"into",total) ; 
*/ 
    PREPARE stmt FROM @SQLQuery; 
    EXECUTE stmt; 
    DEALLOCATE PREPARE stmt;  

END 

это моя процедура. Я могу получить данные при передаче другого параметра. Я хочу получить общее количество строк возврата, и я должен установить выходной параметр, но я получаю нуль, когда я использую мой запрос для подсчета числа записей, которые я есть комментарий, пожалуйста, предложить мне, как получить общее количество записей с помощью динамического запросаКак подсчитать общую запись с помощью динамического запроса в mysql

ответ

1

Если я правильно понимаю, вы бы установить вещи, как это:

set @sql = 'select SQL_CALC_FOUND_ROWS . . . '; 
set @total = -1; 
prepare @sql; 
execute @sql; 

execute 'select found_rows() into @total' ; 

set out_total = @total; 

Примечание: я изменил имя параметра на out_total. Вы должны отличать имена параметров от имен столбцов, используя какое-то соглашение об именах.

+0

set @sql = 'select SQL_CALC_FOUND_ROWS. , , ', что это за линия –

+0

@DevResearch. , , http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_found-rows. –

+0

, где вы должны установить out_total в этой ошибке, выполните execute 'select found_rows() в @total'; –