call spQuery_Lista_Producto_Familia('06', '01', NULL);

DROP PROCEDURE IF EXISTS spQuery_Lista_Producto_Familia;
CREATE PROCEDURE spQuery_Lista_Producto_Familia(
  varLin  varchar(2),
  varCod  varchar(2),
  varNom  varchar(45)
  )
BEGIN
  DECLARE varCadCod, varCadNom TEXT DEFAULT '';
  
	if varCod <> '' and varCod is not null then
    set varCadCod = CONCAT(" and id_familia = '", lpad(varLin,2,'0'), lpad(varCod,2,'0'), "' ");
  end if;
	if varNom <> '' and varNom is not null then
    set varCadNom = CONCAT(" and nombre like '%", varNom,"%' ");
  end if;

 	SET @sql = CONCAT("select substring(id_familia,3,2) as id_familia, nombre ",
                    "from _sv_familia where _estado='A'",
                    varCadCod, varCadNom,
                    "and id_tipo = '", lpad(varLin,2,'0'), "' ",
                    "order by nombre");
  PREPARE s1 FROM @sql;
  EXECUTE s1;
  DEALLOCATE PREPARE s1;
END;