Why does ExtractValue() fail in an UPDATE and succeed in a SELECT? - mysql

When I execute this query...
UPDATE tbl a,
(SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl) b
SET tbl.`url` = b.`url`
...I see this error:
Error Code: 1525
Incorrect XML value: 'parse error at line 57 pos 195: '</div>' unexpected (END-OF-INPUT wanted)'
But when I execute this query...
SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl
...the query succeeds.
Why does the UPDATE query fail where the standalone SELECT query succeeds?

Related

Update query in SQL: command not ended properly

update fare_strategy set sales_date_to = ’07-SEP-22’
where dep_date_to is not null
and market_id in
(select market_id
from fare_strategy_markets
where set_type = ‘STANDARD’
);
In the following query, I'm getting error as SQL command not properly ended.
try to change like this because in your query you used ’ this is not valid so i just change it to ' so it's works fine
update fare_strategy set sales_date_to = '07-SEP-22' where dep_date_to is
not null and market_id in (select market_id from
fare_strategy_markets where set_type = 'STANDARD');

CTE + INSERT INTO + SQLAlchemy

I am trying to insert some date into another table. At first I´ve tried to use sqlalchemy to create such queries, but as I got some error when executing, I tried to solve it through raw SQL, but the error still the same.
I am not very used to CTE commands, so I don´t know if there are some restrinctions over them.
WITH Conv_Pre_Pagos AS
(SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente) SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN
The sentence is bigger, but I removed some data to bring it cleaner. Still, the same error:
ibm_db_dbi::ProgrammingError: SQLNumResultCols failed: [IBM][CLI Driver][DB2] SQL0199N The use of the reserved word "INSERT" following "INSERT" is not valid.
Expected tokens may include: "(SELECT ,". SQLSTATE=42601 SQLCODE=-199
[SQL: WITH Conv_Pre_Pagos AS (SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN]
(Background on this error at: https://sqlalche.me/e/14/f405)"
Where does it see an "insert following insert"?
Try this:
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
WITH Conv_Pre_Pagos AS
(
SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52
)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC
JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN

syntax error (missing operator) in query expression - Access16

I am getting the following error message when attempting to save or run a SQL query in MS Access 2016:
"Syntax error (missing operator) in query expression 'M.ScheduleKey FROM Actual_Data A'
The Query is:
UPDATE Actual_Data
SET A.ScheduleKey = M.ScheduleKey
FROM Actual_Data A, Match M
WHERE A.ActualKey = M.ActualKey
Both the ScheduleKey & ActualKey fields are text fields.
Any help on resolving this would be greatly appreciated.
You can use this instead:
UPDATE Actual_Data A
INNER JOIN Match M
ON A.ActualKey = M.ActualKey
SET A.ScheduleKey = M.ScheduleKey

how to fix sql query error

I have this request
DELETE FROM T1 WHERE PERMISSION_ID = 'x' AND
0<(SELECT * FROM T2 WHERE "SUBSTR"(PID,1,1)=1) OR '1'='1'
but got
The SQL statement " DELETE FROM T1 WHERE PERMISSION_ID = 'x'
AND 0<(SELECT * FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'"
contains the syntax error[s]: - 1:101 - SQL syntax error: the token
"(" was not expected here
Please help, how I can fix it?
UPDATE
remove "SUBSTR" to SUBSTR, but get The SQL statement
DELETE FROM T1
WHERE PERMISSION_ID = 'x'
AND 0= (SELECT * FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'
contains the syntax error[s]: - 1:99 - SQL syntax error: the token "(" was not expected here
UPDATE 2
I change to
SELECT count(*) FROM T2 WHERE SUBSTRING("asdqweasd",1,1))
and get
- SQL syntax error: the token "," was not expected here
- expecting "from", found ','
You do not need Double quotes around SUBSTR. Also you can not use SELECT * . Use count(*)
DELETE FROM T1 WHERE PERMISSION_ID = 'x' AND
0<(SELECT count(*) FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'

Error in hive query Invalid table alias

This is my hive query
INSERT OVERWRITE TABLE bts_monthly_points_liability_rollforward
SELECT currMonth.businessEventType,
prevMonth.totalFaceValue,
prevMonth.totalAccountingValue,
currMonth.earnedFaceValue,
currMonth.earnedAccountingValue,
currMonth.expiredFaceValue,
currMonth.expiredAccountingValue,
currMonth.earnedPointsReturnFaceValue,
currMonth.earnedPointsReturnAccountingValue,
currMonth.spendPointsFaceValue,
currMonth.spendPointsAccountingValue,
currMonth.spendPointsReturnFaceValue,
currMonth.spendPointsReturnAccountingValue,
currMonth.adjustmentFaceValue,
currMonth.adjustmentAccountingValue,
currMonth.totalFaceValue,
currMonth.totalAccountingValue
FROM
(
SELECT business_event_type AS businessEventType,
SUM(earned_face_value) AS earnedFaceValue,
SUM(earned_accounting_value) AS earnedAccountingValue,
SUM(expired_face_value) AS expiredFaceValue,
SUM(expired_accounting_value) AS expiredAccountingValue,
SUM(earned_return_face_value) AS earnedPointsReturnFaceValue,
SUM(earned_return_accounting_value) AS earnedPointsReturnAccountingValue,
SUM(spend_face_value) AS spendPointsFaceValue,
SUM(spend_accounting_value) AS spendPointsAccountingValue,
SUM(spend_return_face_value) AS spendPointsReturnFaceValue,
SUM(spend_return_accounting_value) spendPointsReturnAccountingValue,
CAST(0 AS BIGINT) AS adjustmentFaceValue,
CAST(0 AS BIGINT) AS adjustmentAccountingValue,
(SUM(earned_face_value)+SUM(expired_face_value)+SUM(earned_return_face_value)+SUM(spend_face_value)+SUM(spend_return_face_value)) AS totalFaceValue,
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(currMonth.spend_return_accounting_value)) AS totalAccountingValue
FROM ${pointsApplicationName}_points_balance
WHERE unix_timestamp(accounting_date,'yyyy-MM-dd') >= unix_timestamp("${startDate}",'yyyy-MM-dd') AND unix_timestamp(accounting_date,'yyyy-MM-dd') < unix_timestamp("${endDate}",'yyyy-MM-dd')
GROUP BY business_event_type
)currMonth
JOIN
(
SELECT business_event_type AS businessEventType,
(SUM(earned_face_value)+SUM(expired_face_value)+SUM(earned_return_face_value)+SUM(spend_face_value)+SUM(spend_return_face_value)) AS totalFaceValue,
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(spend_return_accounting_value)) AS totalAccountingValue
FROM ${pointsApplicationName}_points_balance
WHERE unix_timestamp(accounting_date,'yyyy-MM-dd') >= unix_timestamp("${previousMonthStartDate}",'yyyy-MM-dd') AND unix_timestamp(accounting_date,'yyyy-MM-dd') < unix_timestamp("${startDate}",'yyyy-MM-dd')
GROUP BY business_event_type
)prevMonth
ON prevMonth.businessEventType = currMonth.businessEventType;
Error that I am receiving after running this query:
SemanticException [Error 10004]: Line 38:129 Invalid table alias or column reference 'currMonth': (possible column names are: business_event_type, accounting_date, earned_face_value, earned_accounting_value, expired_face_value, expired_accounting_value, earned_return_face_value, earned_return_accounting_value, spend_face_value, spend_accounting_value, spend_return_face_value, spend_return_accounting_value)
Command exiting with ret '255'
The problem is the line 35 of your query. Here's how the query works:
SELECT ...
FROM (
SELECT business_event_type AS businessEventType,
...
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(currMonth.spend_return_accounting_value)) AS totalAccountingValue
FROM ${pointsApplicationName}_points_balance
...
)currMonth
JOIN (...)prevMonth
ON prevMonth.businessEventType = currMonth.businessEventType;
Here you can see that you are using currMonth alias inside of the subquery that aliased as currMonth. The alias does not exist in this context, this is why you get an error. It should be like this:
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(spend_return_accounting_value)) AS totalAccountingValue