-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `apteka`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
                                        select result,
                                        @res := @res + 1 as rownumber
                                        from(
                                        SELECT `t`.`id` AS `t0_c0`, `t`.`shortnameapt` AS `t0_c1`, `t`.`shortnameaptlat` AS `t0_c2`, `t`.`fulnameapt` AS `t0_c3`,
                                        `t`.`prnewprice` AS `t0_c4`, `t`.`datelastmailprice` AS `t0_c5`, `t`.`prioritet` AS `t0_c6`, `t`.`recordcheckfile_id` AS `t0_c7`,
                                        `t`.`datespr` AS `t0_c8`, `t`.`owner` AS `t0_c9`, DATE_FORMAT(t.datespr,"%d/%m/%Y %H:%i") as datespr,
                                        DATE_FORMAT(t.datelastmailprice,"%d/%m/%Y %H:%i") as datelastmailprice, CASE WHEN DATE(ctr.clientsite_dateo)>DATE(NOW()) THEN "+" ELSE "-" END as clientsite_exist,
                                        CASE WHEN DATE(ctr.clientcficity_dateo)>DATE(NOW()) THEN "+" ELSE "-" END as clientcficity_exist,
                                        CASE WHEN DATE(ctr.clientmail_dateo)>DATE(NOW()) THEN "+" ELSE "-" END as clientmail_exist,
                                        CASE WHEN DATE(ctr.clientsitekarta_dateo)>DATE(NOW()) THEN "+" ELSE "-" END as clientsitekarta_exist,
                                        CASE WHEN DATE(ctr.clientcficityino_dateo)>DATE(NOW()) THEN "+" ELSE "-" END as clientcficityino_exist,
                                        t.owner as owner, aaCoordinatesRegion.geoobject as region, aaCoordinatesRegion.id as region_id, aCoordinatesCity.geoobject as city,
                                        aCoordinatesCity.id as city_id, aCoordinatesRaion.geoobject as raion, aCoordinatesRaion.id as raion_id, aOwner.nick_name as nick_name,
                                        CASE WHEN aInfo.proizv=1 THEN "+" ELSE "-" END as proizv, CASE WHEN aInfo.gomeo=1 THEN "+" ELSE "-" END as gomeo,
                                        CASE WHEN aInfo.municipal=1 THEN "+" ELSE "-" END as municipal, CASE WHEN aInfo.prokat=1 THEN "+" ELSE "-" END as prokat,
                                        CASE WHEN aInfo.veterinar=1 THEN "+" ELSE "-" END as veterinar, CASE WHEN aInfo.kruglosut=1 THEN "+" ELSE "-" END as kruglosut,
                                        CASE WHEN aInfo.dejurn=1 THEN "+" ELSE "-" END as dejurn, CASE WHEN aDiscounts.skidki is not null THEN "+" ELSE "-" END as skidki,
                                        `ctr`.`apteka_id` AS `t1_c0`, `ctr`.`clientcficity` AS `t1_c1`, `ctr`.`clientcficity_daten` AS `t1_c2`, `ctr`.`clientcficity_dateo` AS `t1_c3`,
                                        `ctr`.`clientsite` AS `t1_c4`, `ctr`.`clientsite_daten` AS `t1_c5`, `ctr`.`clientsite_dateo` AS `t1_c6`, `ctr`.`clientmail` AS `t1_c7`,
                                        `ctr`.`clientmail_daten` AS `t1_c8`, `ctr`.`clientmail_dateo` AS `t1_c9`, `ctr`.`clientsitekarta` AS `t1_c10`,
                                        `ctr`.`clientsitekarta_daten` AS `t1_c11`, `ctr`.`clientsitekarta_dateo` AS `t1_c12`, `ctr`.`clientcficityino` AS `t1_c13`,
                                        `ctr`.`clientcficityino_daten` AS `t1_c14`, `ctr`.`clientcficityino_dateo` AS `t1_c15`, `ctr`.`datespr` AS `t1_c16`,
                                        `ctr`.`owner` AS `t1_c17`, CASE WHEN DATE_FORMAT(clientcficity_daten,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientcficity_daten,"%d/%m/%Y") END as clientcficity_daten,
                                        CASE WHEN DATE_FORMAT(clientsite_daten,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientsite_daten,"%d/%m/%Y") END as clientsite_daten,
                                        CASE WHEN DATE_FORMAT(clientmail_daten,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientmail_daten,"%d/%m/%Y") END as clientmail_daten,
                                        CASE WHEN DATE_FORMAT(clientsitekarta_daten,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientsitekarta_daten,"%d/%m/%Y") END as clientsitekarta_daten,
                                        CASE WHEN DATE_FORMAT(clientcficityino_daten,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientcficityino_daten,"%d/%m/%Y") END as clientcficityino_daten,
                                        CASE WHEN DATE_FORMAT(clientcficity_dateo,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientcficity_dateo,"%d/%m/%Y") END as clientcficity_dateo,
                                        CASE WHEN DATE_FORMAT(clientsite_dateo,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientsite_dateo,"%d/%m/%Y") END as clientsite_dateo,
                                        CASE WHEN DATE_FORMAT(clientmail_dateo,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientmail_dateo,"%d/%m/%Y") END as clientmail_dateo,
                                        CASE WHEN DATE_FORMAT(clientsitekarta_dateo,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientsitekarta_dateo,"%d/%m/%Y") END as clientsitekarta_dateo,
                                        CASE WHEN DATE_FORMAT(clientcficityino_dateo,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(clientcficityino_dateo,"%d/%m/%Y") END as clientcficityino_dateo,
                                        `aInfo`.`apteka_id` AS `t2_c0`, `aInfo`.`proizv` AS `t2_c1`, `aInfo`.`gomeo` AS `t2_c2`, `aInfo`.`municipal` AS `t2_c3`, `aInfo`.`prokat` AS `t2_c4`,
                                        `aInfo`.`veterinar` AS `t2_c5`, `aInfo`.`kruglosut` AS `t2_c6`, `aInfo`.`dejurn` AS `t2_c7`, `aInfo`.`datespr` AS `t2_c8`, `aInfo`.`owner` AS `t2_c9`,
                                        `aaCoordinatesRegion`.`id` AS `t3_c0`, `aaCoordinatesRegion`.`geoobject` AS `t3_c1`, `aaCoordinatesRegion`.`parent_id` AS `t3_c2`, `aCoordinatesCity`.`id` AS `t6_c0`,
                                        `aCoordinatesCity`.`geoobject` AS `t6_c1`, `aCoordinatesCity`.`parent_id` AS `t6_c2`, `aCoordinatesRaion`.`id` AS `t8_c0`, `aCoordinatesRaion`.`geoobject` AS `t8_c1`,
                                        `aCoordinatesRaion`.`parent_id` AS `t8_c2`, `aDiscounts`.`apteka_id` AS `t10_c0`, `aDiscounts`.`skidki` AS `t10_c1`, `aDiscounts`.`datenskidki` AS `t10_c2`,
                                        `aDiscounts`.`dateoskidki` AS `t10_c3`, `aDiscounts`.`datespr` AS `t10_c4`, `aDiscounts`.`owner` AS `t10_c5`,
                                        CASE WHEN DATE_FORMAT(datenskidki,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(datenskidki,"%d/%m/%Y") END as datenskidki,
                                        CASE WHEN DATE_FORMAT(dateoskidki,"%d/%m/%Y") ="01/01/1970" THEN "" ELSE DATE_FORMAT(dateoskidki,"%d/%m/%Y") END as dateoskidki,
                                        `aOwner`.`id` AS `t11_c0`, `aOwner`.`change_date` AS `t11_c1`, `aOwner`.`first_name` AS `t11_c2`, `aOwner`.`middle_name` AS `t11_c3`, `aOwner`.`last_name` AS `t11_c4`,
                                        `aOwner`.`nick_name` AS `t11_c5`, `aOwner`.`email` AS `t11_c6`, `aOwner`.`gender` AS `t11_c7`, `aOwner`.`birth_date` AS `t11_c8`, `aOwner`.`site` AS `t11_c9`,
                                        `aOwner`.`about` AS `t11_c10`, `aOwner`.`location` AS `t11_c11`, `aOwner`.`status` AS `t11_c12`, `aOwner`.`access_level` AS `t11_c13`, `aOwner`.`last_visit` AS `t11_c14`,
                                        `aOwner`.`registration_date` AS `t11_c15`, `aOwner`.`avatar` AS `t11_c16`, `aOwner`.`hash` AS `t11_c17`, `aOwner`.`email_confirm` AS `t11_c18`,
                                        @id := @id + 1 AS rownumber ,
                                        case when',' ',fieldname ,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
											FROM `spr_apteka` `t`
                                        LEFT OUTER JOIN `spr_apteka_dogovor` `ctr` ON (`t`.`id`=`ctr`.`apteka_id`)
                                        LEFT OUTER JOIN `spr_apteka_type` `aInfo` ON (`t`.`id`=`aInfo`.`apteka_id`)
                                        LEFT OUTER JOIN `spr_apteka_koord` `aaaCoordinates` ON (`t`.`id`=`aaaCoordinates`.`apteka_id`)
                                        LEFT OUTER JOIN `spr_geo` `aCoordinatesRegion` ON (`aCoordinatesRegion`.`id`=`aaaCoordinates`.`city_id`)
                                        LEFT OUTER JOIN `spr_geo` `aaCoordinatesRegion` ON (`aaCoordinatesRegion`.`id`=`aCoordinatesRegion`.`parent_id`)
                                        LEFT OUTER JOIN `spr_apteka_koord` `aCoordinates` ON (`t`.`id`=`aCoordinates`.`apteka_id`)
                                        LEFT OUTER JOIN `spr_geo` `aCoordinatesCity` ON (`aCoordinatesCity`.`id`=`aCoordinates`.`city_id`)
                                        LEFT OUTER JOIN `spr_apteka_koord` `aaCoordinates` ON (`t`.`id`=`aaCoordinates`.`apteka_id`)
                                        LEFT OUTER JOIN `spr_geo` `aCoordinatesRaion` ON (`aCoordinatesRaion`.`id`=`aaCoordinates`.`raion_id`)
                                        LEFT OUTER JOIN `spr_apteka_skidka` `aDiscounts` ON (`t`.`id`=`aDiscounts`.`apteka_id`)
                                        LEFT OUTER JOIN `yupe_user_user` `aOwner` ON (`t`.`owner`=`aOwner`.`id`) ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `baggers`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
                                    select result,
                                    @res := @res + 1 as rownumber
                                        from(
                                            SELECT
                                            t.*,
                                            @id := @id + 1 AS rownumber ,
                                            CASE WHEN ',fieldname,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
                                    FROM apteki_dbf.spr_tnfasovka as t
                                            left join apteki_dbf.yupe_user_user as aOwner ON t.owner = aOwner.id ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `commentary`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
						select result,
						@res := @res + 1 as rownumber
							from(
                                    SELECT `t`.`commentary` AS `t0_c1`, `t`.`owner` AS `t0_c4`, `t`.`datespr` AS `t0_c3`,
                                    aOwner.nick_name as nick_name, `t`.`id` AS `t0_c0`,
                                    `aOwner`.`id` AS `t1_c0`, `aOwner`.`change_date` AS `t1_c1`,
                                     `aOwner`.`first_name` AS `t1_c2`,
                                    `aOwner`.`middle_name` AS `t1_c3`,
                                     `aOwner`.`last_name` AS `t1_c4`,
                                    `aOwner`.`nick_name` AS `t1_c5`,
                                     `aOwner`.`email` AS `t1_c6`,
                                     `aOwner`.`gender` AS `t1_c7`,
                                    `aOwner`.`birth_date` AS `t1_c8`,
                                    `aOwner`.`site` AS `t1_c9`,
                                    `aOwner`.`about` AS `t1_c10`,
                                     `aOwner`.`location` AS `t1_c11`,
                                    `aOwner`.`status` AS `t1_c12`,
                                    `aOwner`.`access_level` AS `t1_c13`,
                                     `aOwner`.`last_visit` AS `t1_c14`,
                                    `aOwner`.`registration_date` AS `t1_c15`,
                                    `aOwner`.`avatar` AS `t1_c16`,
                                     `aOwner`.`hash` AS `t1_c17`,
                                    `aOwner`.`email_confirm` AS `t1_c18` ,
                                    @id := @id + 1 AS rownumber ,
                                    CASE WHEN ',fieldname,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
                                    FROM `spr_tncommentary` `t`
                                    LEFT OUTER JOIN `yupe_user_user` `aOwner` ON (`t`.`owner`=`aOwner`.`id`) ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END


-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `fg`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
                                    select result,
                                    @res := @res + 1 as rownumber
                                        from(
                                            SELECT
                                            @id := @id + 1 AS rownumber ,
                                            spr_fg.*,

                                            CASE WHEN',' ',fieldname ,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
											FROM apteki_dbf.spr_fg
                                            left join apteki_dbf.yupe_user_user as aOwner ON apteki_dbf.spr_fg.owner = aOwner.id ',wheree,' ','group by spr_fg.fg ',
                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END


-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `forma`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
                                    select result,
                                    @res := @res + 1 as rownumber
                                        from(
                                            SELECT
                                            t.*,
                                            @id := @id + 1 AS rownumber ,
                                            CASE WHEN ',fieldname,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
											FROM apteki_dbf.spr_tndrugforma as t
                                            left join apteki_dbf.yupe_user_user as aOwner ON t.owner = aOwner.id ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END


-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `measuring`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
                                    select result,
                                    @res := @res + 1 as rownumber
                                        from(
                                            SELECT
                                            t.owner,t.datespr,
                                            t.tndoza_v_ed_df as measuring,
                                            @id := @id + 1 AS rownumber ,
                                            CASE WHEN ',fieldname,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
                                    FROM apteki_dbf.spr_tndoza_v_ed_df as t
                                            left join apteki_dbf.yupe_user_user as aOwner ON t.owner = aOwner.id ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END


-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `mnn`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat(
 'select rownumber from(
                                   select result,
                                    @res := @res + 1 as rownumber
                                        from(
                                            SELECT
                                            t.*,
                                            @id := @id + 1 AS rownumber ,
                                            CASE WHEN ',fieldname,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
                                            FROM apteki_dbf.spr_mnn as t
                                            left join apteki_dbf.yupe_user_user as aOwner ON t.owner = aOwner.id',
                                            wheree,' ',orderby, '
                                        ) as rr
                                ) as rrr where result>0 ',
                                limitt);




PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END


-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `packing`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
                                    select result,
                                    @res := @res + 1 as rownumber
                                        from(
                                            SELECT
                                            t.*,
                                            @id := @id + 1 AS rownumber ,
                                            CASE WHEN ',fieldname,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
                                    FROM apteki_dbf.spr_tnupakovka as t
                                            left join apteki_dbf.yupe_user_user as aOwner ON t.owner = aOwner.id ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END


-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `price`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
						select result,
						@res := @res + 1 as rownumber
							from(
                                    SELECT t.id as id, t.apteka_id as apteka_id, t.tnname as tnname,
                                    t.price as price, t.mnn as mnn, t.country as country,
                                    t.manufactory as manufactory, t.barcode as barcode,
                                    t.surplus as surplus, t.date_in as date_in, t.date_out as date_out,
                                    t.opt_price as opt_price, t.enter_code as enter_code, t.batch as batch,
                                    t.drug_group as drug_group, apteka.fulnameapt as apteka,
                                    CASE WHEN tn_id is null THEN "empty" ELSE tn_id END as tn_id,
                                    date_format(t.datespr,"%d/%m/%y") as datespr,
                                    tnfull.tn as tn, `t`.`id` AS `t0_c0`, `tnfull`.`id` AS `t1_c0`,
                                    `tnfull`.`tn` AS `t1_c1`, `tnfull`.`nai` AS `t1_c2`, `tnfull`.`pn` AS `t1_c3`,
                                    `tnfull`.`recept` AS `t1_c4`, `tnfull`.`gv` AS `t1_c5`, `tnfull`.`pku` AS `t1_c6`,
                                    `tnfull`.`lg` AS `t1_c7`, `tnfull`.`spisok2` AS `t1_c8`, `tnfull`.`doza_id` AS `t1_c9`,
                                    `tnfull`.`fasovka_id` AS `t1_c10`, `tnfull`.`drugforma_id` AS `t1_c11`, `tnfull`.`upakovka_id` AS `t1_c12`,
                                    `tnfull`.`commentary_id` AS `t1_c13`, `tnfull`.`mnn_id` AS `t1_c14`, `tnfull`.`tnshort_id` AS `t1_c15`,
                                    `tnfull`.`fg_id` AS `t1_c16`, `tnfull`.`ftg_id` AS `t1_c17`, `tnfull`.`datespr` AS `t1_c18`,
                                    `tnfull`.`owner` AS `t1_c19`, `apteka`.`id` AS `t2_c0`, `apteka`.`shortnameapt` AS `t2_c1`,
                                    `apteka`.`shortnameaptlat` AS `t2_c2`, `apteka`.`fulnameapt` AS `t2_c3`, `apteka`.`prnewprice` AS `t2_c4`,
                                    `apteka`.`datelastmailprice` AS `t2_c5`, `apteka`.`prioritet` AS `t2_c6`, `apteka`.`recordcheckfile_id` AS `t2_c7`,
                                    `apteka`.`datespr` AS `t2_c8`, `apteka`.`owner` AS `t2_c9`,

                                    @id := @id + 1 AS rownumber ,
                                    CASE WHEN',' ',fieldname ,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
											FROM `price_prices_all` `t`
                                    LEFT OUTER JOIN `spr_tnfull` `tnfull` ON (`tnfull`.`id`=`t`.`tn_id`)
                                    LEFT OUTER JOIN `spr_apteka` `apteka` ON (`apteka`.`id`=`t`.`apteka_id`) ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END


-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `shortname`(IN fieldname text, IN param2 text,IN wheree text,IN orderby text,IN limitt text)
BEGIN
SET @res:=0;
SET @id:=0;
SET @s=concat('select rownumber from(
                                    select result,
                                    @res := @res + 1 as rownumber
                                        from(
                                            SELECT
                                            t.*,
                                            @id := @id + 1 AS rownumber ,
                                            CASE WHEN ',fieldname,' like \'',param2,'\'',' THEN @id ELSE 0 END as result
											FROM apteki_dbf.spr_tnshort as t
                                            left join apteki_dbf.yupe_user_user as aOwner ON t.owner = aOwner.id ',wheree,' ',

                                    orderby,'
                                    ) as rr
                                    ) as rrr where result>0 ',limitt);
PREPARE mselect FROM @s;
EXECUTE mselect;
deallocate PREPARE mselect;
END

