Convert SQL query to eloquent query for update table in Laravel - laravel

I want to convert this SQL query to an eloquent query for the update table but get the error ORA-00971: missing SET keyword
UPDATE services
SET services.statement_id = (
SELECT statements.id
FROM statements
WHERE services.service_ref = statements.statement_ref
)
this my eloquent query
DB::select('UPDATE services SET services.statement_id = (SELECT statements.id
FROM statements
WHERE services.service_ref = statements.statement_ref)');
I want to update data with join laravel

use :
DB::select(DB::raw('UPDATE services SET services.statement_id = (SELECT statements.id
FROM statements
WHERE services.service_ref = statements.statement_ref)'));

Related

Oracle not using index, Entity Framework & Devart DotConnect for oracle

The table in question has ~30mio records. Using Entity Framework I write a LINQ Query like this:
dbContext.MyTable.FirstOrDefault(t => t.Col3 == "BQJCRHHNABKAKU-KBQPJGBKSA-N");
Devart DotConnect for Oracle generates this:
SELECT
Extent1.COL1,
Extent1.COL2,
Extent1.COL3
FROM MY_TABLE Extent1
WHERE (Extent1.COL3 = :p__linq__0) OR ((Extent1.COL3 IS NULL) AND (:p__linq__0 IS NULL))
FETCH FIRST 1 ROWS ONLY
The query takes about four minutes, obviously a full table scan.
However, handcrafting this SQL:
SELECT
Extent1.COL1,
Extent1.COL2,
Extent1.COL3
FROM MY_TABLE Extent1
WHERE Extent1.COL3 = :p__linq__0
FETCH FIRST 1 ROWS ONLY
returns the expected match in 200ms.
Question: Why is it so? I would expect the query optimizer to note that the right part is false if the parameter is not null, so why doesn't the first query hit the index?
Please set UseCSharpNullComparisonBehavior=false explicitly:
var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
config.QueryOptions.UseCSharpNullComparisonBehavior = false;
If this doesn't help, send us a small test project with the corresponding DDL script so that we can investigate the issue.

Where Not In using query builder

Similary with this question I need to execute the following Sql query:
SELECT COUNT(*) from table where column NOT IN (SELECT table2.id from table2 where table2.someanothercolumn >0 );
Using Eloquent's query builder, therefore I tried the following (Model Table maps into table table and Model TableTwo maps into table table2):
$enties = Table::where('id',function($q){
$q->from('table2')->select('id')->where('someanothercolumn','>',0);
})->count();
But on the cide above how I can Place the NOT IN clause?
Your answer is in the following snippet of code:
$enties = Table::whereNotIn('id',function($q){
$q->from('table2')->select('id')->where('someanothercolumn','>',0);
})->count();
In other words just use the whereNotIn.

Complex SQL Query laravel Build

How would I use query builder in Laravel to generate the following SQL statement:
SELECT MAX(QTE) FROM (SELECT SUM(activity_sale_report.quantity_sold)
AS QTE FROM activity_sale_report
GROUP BY activity_sale_report.activity_id) AS T
If you are just using the query builder and not models you can use raw queries
$query = DB::select( DB::raw("SELECT MAX(QTE) FROM (SELECT SUM(activity_sale_report.quantity_sold) AS QTE FROM activity_sale_report GROUP BY activity_sale_report.activity_id) AS T);
I would recommend using the eloquent models, it's easier.

How can I convert sql to linq

This is my SQL query
SELECT
sys.sysobjects.name Name,
sys.foreign_keys.*
FROM
sys.foreign_keys
inner join sys.sysobjects on
sys.foreign_keys.parent_object_id = sys.sysobjects.id
WHERE
referenced_object_id = OBJECT_ID(N'[dbo].[Country]')
I have installed Linqer to convert SQL to linq.
But I got an error:
SQL cannot be converted to LINQ: Table [foreign_keys] not found in the current Data Context.
I am a beginner in Linq. Can Anyone help me to convert SQL to Linq
The problem is that system views will not be picked up by Linqer. If you want to read these tables in your application, first create your own views on them, as was done here and write a query on these views.
CREATE VIEW SysObjectsView AS SELECT * FROM sys.sysobjects;
GO
CREATE VIEW SysForeignKeysView AS SELECT * FROM sys.foreign_keys;
GO
SELECT obj.name Name, fk.*
FROM SysForeignKeysView fk
INNER JOIN SysObjectsView obj ON fk.parent_object_id = obj.id
INNER JOIN SysObjectsView objfk ON fk.referenced_object_id = objfk.id
WHERE objfk.name = N'Country'
Linqer should be able to pick up these views.

Oracle: update using values from other tables

I've got the following Update but I don't want to copy the values manually.
I prefer to extract them using a query.
Should I use PL/SQL or is there an SQL formulation?
The query that I use to obtain the values I've copied manually into the update is not key preserving ("Key-preserved table concept in join view").
UPDATE wkf_cronologia
SET swkf_stato_workflow_id = 'o3gE1tlSdcDIC6FF',
swkf_data_ini = TO_TIMESTAMP ('19-06-2010 18:28:10,556000000','DD-MM-RRRR HH24:MI:SS,FF'),
swkf_versione = 0,
SPWKF_STATO_PUBBLICO_ID = '*1UNICOO',
SPWKF_DATA_INI = TO_TIMESTAMP ('01-01-0001 00:00:00,000000000', 'DD-MM-RRRR HH24:MI:SS,FF'),
SPWKF_VERSIONE = 0
WHERE wkfc_cronologia_id = 'ApAJ0qCudNphjLxj';
You can use a subquery:
UPDATE wkf_cronologia
SET (swkf_stato_workflow_id,
swkf_data_ini,
swkf_versione,
SPWKF_STATO_PUBBLICO_ID,
SPWKF_DATA_INI,
SPWKF_VERSIONE) = (select a,b,0,c,d,e
from another_table
where something=wkf_cronologia.swkf_stato_workflow_id)
WHERE swkf_versione is null;

Resources