mercredi 11 juillet 2018

How to run a raw query in Laravel 5.6

My laravel is to a mssql database connected. Now i would like execute a bigger query string.

MERGE PRODUCT_DETAILS AS TARGET
USING UPDATED_DETAILS AS SOURCE 
ON (TARGET.P_ID = SOURCE.P_ID)              


THEN MATCHED AND TARGET.P_NAME  SOURCE.P_NAME 
OR TARGET.P_PRICE  SOURCE.P_PRICE THEN 

//when records are matched (on the basis  
//of P_ID) then do the update operation 
UPDATE SET TARGET.P_Name = SOURCE.P_NAME,    

//if there are changes in P_NAME OR P_PRICE
TARGET.P_PRICE = SOURCE.P_PRICE         

WHEN NOT MATCHED BY TARGET THEN  

//When no records are matched with target table 
//then insert the records in the target table
INSERT (P_ID,P_NAME,P_PRICE)          
VALUES (SOURCE.P_ID,SOURCE.P_NAME,SOURCE.P_PRICE)

WHEN NOT MATCHED BY SOURCE THEN         

//when no records are matched with source table 
//the delete that record in target table
DELETE;

Something like this. It's just an example of mssql merge query. My query is 4 times bigger but the concept is the same.

I wraped the whole string into the \DB::raw("....") but this doesn't work. I also tried this to run with \DB::select("...") but this also failed.

How can i execute this raw query string in laravel ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire