How to run SQL Update query in NiFi PUTSQL? - mysql

I want to update a column enum_value of tableB with the values of another column enum_value in tableA using MySQL update query as follows:
UPDATE tableB t1
INNER JOIN TableA t2 ON t1.sig_name = t2.sig_name
SET t1.enum_value = t2.enum_value
WHERE t1.dbc_Version = t2.dbc_version
The above SQL query runs fine in the MYSQL workbench, but I want to execute this query (perform this Update dynamically using NiFi PUTSQL (SQL Statement) property. When I write this in NiFi PutSQL, I get an error. I have attached the screenshots below:
Is there I way I can achieve dynamic(on the fly update in the DB) using NiFi? If yes, Then how and what controller services needThanks in advance! to be set?
Thanks in advance!

If you have the query directly in PutSQL, what is the purpose of the ConvertJSONToSQL processor right before that? That processor generates attributes from the JSON data, which PutSQL looks for when trying to populate a prepared statement with parameters (which you don't have as you're using an explicit statement with no ?s).
See this SO post for more information, basically you should use UpdateAttribute to remove any attributes created by ConvertJSONToSQL.

Related

MySql Backslashes disappear when data is updated to another table

I have a table that contains directory paths and the data looks like this:
But when I run an update statement where I join another table to this one and update the to existing rows in the new table, the backslashes disappear like this:
This is the update statement, where
"MIJob.SourceFile"
has the proper text containing the backslashes, and
"MIJobFileLocation.Path_Folder"
is the column being updated and does not have backslashes in its data.
This is the update statement:
UPDATE MIJobFileLocation
INNER JOIN MIJob
ON MIJobFileLocation.MIJobFileLocationGUID = MIJob.MIJobFileLocationGUID_Source
SET
MIJobFileLocation.Path_Folder = MIJob.SourceFile
WHERE MIJob.SourceFile IS NOT NULL
This SQL will run in a stored procedure in MySQL. How can I preserve the backslashes?
I’ve been googling this for hours with no success.
Thank you.
Execute this line before your update statement. Seems like a hack solution, but it works.
SET SESSION sql_mode='NO_BACKSLASH_ESCAPES'
UPDATE sometable
set.....

how to include hard-coded value to output from mysql query?

I've created a MySQL sproc which returns 3 separate result sets. I'm implementing the npm mysql package downstream to exec the sproc and get a result structured in json with the 3 result sets. I need the ability to filter the json result sets that are returned based on some type of indicator in each result set. For example, if I wanted to get the result set from the json response which deals specifically with Suppliers then I could use some type of js filter similar to this:
var supplierResultSet = mySqlJsonResults.filter(x => x.ResultType === 'SupplierResults');
I think SQL Server provides the ability to include a hard-coded column value in a SQL result set like this:
select
'SupplierResults',
*
from
supplier
However, this approach appears to be invalid in MySQL b/c MySQL Workbench is telling me that the sproc syntax is invalid and won't let me save the changes. Do you know if something like what I'm trying to achieve is possible in MySQL and if not then can you recommend alternative approaches that would help me achieve my ultimate goal of including some type of fixed indicator in each result set to provide a handle for downstream filtering of the json response?
If I followed you correctly, you just need to prefix * with the table name or alias:
select 'SupplierResults' hardcoded, s.* from supplier s
As far as I know, this is the SQL Standard. select * is valid only when no other expression is added in the selec clause; SQL Server is lax about this, but most other databases follow the standard.
It is also a good idea to assign a name to the column that contains the hardcoded value (I named it hardcoded in the above query).
In MySQL you can simply put the * first:
SELECT *, 'SupplierResults'
FROM supplier
Demo on dbfiddle
To be more specific, in your case, in your query you would need to do this
select
'SupplierResults',
supplier.* -- <-- this
from
supplier
Try this
create table a (f1 int);
insert into a values (1);
select 'xxx', f1, a.* from a;
Basically, if there are other fields in select, prefix '*' with table name or alias

Update Case-Sensitive DB Field In Laravel 5.3 With Postgres

I am trying to update a database column field with raw SQL in laravel. It's important to mention that the update code was written to MySQL drive but now I use Postgres. The column name is dayID. So the update code is:
DB::update("update table set travel = ... WHERE dayID = {$this->dayID}");
I must use raw SQL because I make some updates to polygon types.
The problem is that laravel automatically transforms the dayID to dayid so I get an error:
column "dayid" does not exist
I tried to set a variable in order to use it in update query but it also failed with the same error:
$var = "dayID";
DB::update("update table set travel = ... WHERE ".$var." = {$this->dayID}");
How can I fix it?
Please try DB::table with update below:
DB::table('table_name')
->where('dayID', $this->dayID)
->update(['travel' => '...']);
Laravel document :
https://laravel.com/docs/5.3/queries#updates

phpmyadmin SQL query multiple tables

I have two tables.
(1) compressors
(2) crankcase_heaters
I'm trying to create a SQL query to do:
Select the compressor.VOLTAGE and compressor.WATT of each compressor.PART_NUMBER
Find the crankcase_heater.PARTNO that has the same voltage and watts.
Add that value into a new field on the compressor table called "CRANKHTR"
Essentially this query will reproduce my compressors table but will have another 'column' called "CRANKHTR".
I'm completely lost on where to even start with this. I tried using the phpmyadmin SQL Query builder but i have no idea where to begin.
Without seeing the exact data structure, it sounds like you need a simple INNER JOIN:
SELECT
`cp`.`VOLTAGE`,
`cp`.`WATT`,
`ch`.`PARTNO` as CRANKHTR
FROM
`compressor` cp
INNER JOIN `crankcase_heaters` ch ON ch.VOLTAGE = cp.VOLTAGE AND ch.WATT = cp.WATT

How does linq-to-sql generate sql for collection pseudoqueries?

My understanding is that the LinqToSql pseudolanguage describes a set using a syntax very similar to SQL and this will allow you to efficiently update a property on a collection of objects:
from b in BugsCollection where b.status = 'closed' set b.status = 'open'
This would update the underlying database using just one SQL statement.
Normally an ORM needs to retieve all of the rows as separate objects, update attributes on each of them and save them individually to the database (at least that's my understanding).
So, how does linq-to-sql avoid having to do this when other orms are not able to avoid it?
The syntax shown in your question is incorrect. LINQ is not intended to have side-effects; it is a query language. The proper way to accomplish what you're looking for is
var x = from b in dataContext.BugsCollection where b.status == "closed";
foreach (var y in x)
y.status = "open";
dataContext.SubmitChanges();
This would generate the single SQL statement that you're talking about. The reason it is able to accomplish this is because of deferred execution - the L2S engine doesn't actually talk to the database until it has to - in this case, because SubmitChanges() was called. L2S then sends the generated SQL statement to the database for execution.
Because LINQ to SQL uses Expression Trees to convert your Query Syntax to actual SQL...it then executes the SQL against the database (rather than pulling all of the data, executing against the in-memory data, and then writing the changes back to the database).
For example, the following Query Syntax:
var records = from r in Records
where r.Property == value
select r
Gets translated first to Lamda Syntax:
Records.Where(r => r.Property == value).Select();
And finally to SQL (via Expression Trees):
SELECT Property, Property2, Property3 FROM Record WHERE Property = #value
...granted, the example doesn't update anything...but the process would be the same for an update query as opposed to a simple select.