How to implement oracle query to select from different tables in laravel
I'm trying to select distinct from different tables in laravel. In oracle I have implemented successfully the query and works. Now I'm trying to "translate" it into laravel. How can I do that?
In oraclesql the query is the following. TEMP table exists as well as all other tables used in this query. Is it possible to do this with DB::raw? Could you give me your advice please?
INSERT INTO TEMP (OBJECT_TYPE, OBLECT_ID)
SELECT DISTINCT HRP1001_CG.OBJECT_TYPE, HRP1001_CG.OBJECT_ID FROM HRP1001_SC, HRP1001_CG, CONFIG
WHERE
(HRP1001_SC.OBJECT_TYPE = 'CG')
AND
(HRP1001_SC.REL_OBJ_TYPE = 'SC')
AND
(HRP1001_SC.REL_OBJ_ID = CONFIG.SC)
AND
((HRP1001_SC.ST_DATE < CONFIG.DES_DATE) AND (HRP1001_SC.END_DATE > CONFIG.DES_DATE))
AND
(HRP1001_CG.REL_OBJ_ID = HRP1001_SC.OBJECT_ID)
AND
((HRP1001_CG.OBJECT_TYPE ='CG') OR (HRP1001_CG.OBJECT_TYPE ='SM'))
ORDER BY HRP1001_CG.OBJECT_ID;
@TRAY2 - At least for now I don't want to rename tables since they are directly used by my oracle database.
Although I do not get any error when running this query, my temp table doesn't populated (it should do however!). What else could I do?
DB::raw("insert into temp (object_type, oblect_id)
select distinct hrp1001_cg.object_type, hrp1001_cg.object_id from hrp1001_sc, hrp1001_cg, config
where
(hrp1001_sc.object_type = 'cg')
and
(hrp1001_sc.rel_obj_type = 'sc')
and
(hrp1001_sc.rel_obj_id = config.sc)
and
((hrp1001_sc.st_date < config.des_date) and (hrp1001_sc.end_date > config.des_date))
and
(hrp1001_cg.rel_obj_id = hrp1001_sc.object_id)
and
((hrp1001_cg.object_type ='cg') or (hrp1001_cg.object_type ='sc'))
order by hrp1001_cg.object_id;");
And when replacing 'cg' with "cg" I get error again:
Error Code : 911 Error Message : ORA-00911: invalid character Position : 606 Statement : insert into temp (object_type, oblect_id) select distinct hrp1001_cg.object_type, hrp1001_cg.object_id from hrp1001_sc, hrp1001_cg, config where (hrp1001_sc.object_type = "cg") and (hrp1001_sc.rel_obj_type = "sc") and (hrp1001_sc.rel_obj_id = config.sc) and ((hrp1001_sc.st_date < config.des_date) and (hrp1001_sc.end_date > config.des_date)) and (hrp1001_cg.rel_obj_id = hrp1001_sc.object_id) and ((hrp1001_cg.object_type ="cg") or (hrp1001_cg.object_type ="sc")) order by hrp1001_cg.object_id; Bindings : [] (911)
I have started to wonder if it is possible to do it. I have tried many many combinations with no success...
@TRAY2 - In fact this is a part of a bigger query. If I succeed with this excerpt I wil proceed with my code. Temp data will be used afterwards. Now I'm badly stuck and cannot proceed with my work.
@TRAY2 - Thank you for your advice!
The ideal would be to make the DB:raw to work somehow . I can't understand what's wrong with it. I've asked for help at stackoverflow too, but you are the one and only who has responded. Thank you so much for that.
I didn't know about the db procedures. I'll try to search about how to.
I have to find a way fast.
@TRAY2 - Finally I figured out the solution. It was simple. My mistake was that I was directly inserting the whole query into DB. I should have first assign it to string. So below is my code excerpt which is tested and working.
$q = "insert into temp(object_type, object_id)";
$q = $q."select distinct hrp1001_cg.object_type, hrp1001_cg.object_id from
hrp1001_cg, hrp1001_sc, config where";
$q =$q." hrp1001_sc.object_type = 'CG'";
$q = $q." and hrp1001_sc.rel_obj_type = 'SC'";
$q = $q." and hrp1001_sc.rel_obj_id = config.sc";
$q = $q." and hrp1001_sc.st_date < config.des_date";
$q = $q." and hrp1001_sc.end_date > config.des_date";
$q = $q." and hrp1001_cg.rel_obj_id = hrp1001_sc.object_id";
$q = $q." and";
$q = $q." ((hrp1001_cg.object_type = 'CG') or (hrp1001_cg.object_type = 'SM'))";
$xx = DB::insert($q);
@TRAY2 - Me too ;).
I'd be much happier if I could do it with eloquent and no raws, but still it's a decent solution. At least I'm declaring tables with schemas. I have a long way to go!