Update SQL with two tables in Oracle - sql

I have a sql like this
UPDATE A
SET A.TEMSILCI_KOD = 4
FROM S_MUSTERI A, S_TEKLIF B
WHERE A.TEMSILCI_KOD = 9
AND B.BAYI_KOD = 17
AND A.HESAP_NO = B.HESAP_NO
But i getting an error like this
Error starting at line 8 in command:
UPDATE A
SET A.TEMSILCI_KOD = 4
FROM S_MUSTERI A, S_TEKLIF B
WHERE A.TEMSILCI_KOD = 9
AND B.BAYI_KOD = 17
AND A.HESAP_NO = B.HESAP_NO
Error at Command Line:9 Column:22
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Where is the ERROR?

Maybe something like
UPDATE S_MUSTERI
SET TEMSILCI_KOD = 4
WHERE TEMSILCI_KOD = 9
AND EXISTS (SELECT 1 FROM S_TEKLIF B
WHERE S_MUSTERI.HESAP_NO = B.HESAP_NO
AND B.BAYI_KOD = 17)

In Oracle the syntax to update a view is different from SQL*Server's syntax. In Oracle you could issue the following query:
UPDATE (SELECT A.TEMSILCI_KOD
FROM S_MUSTERI A, S_TEKLIF B
WHERE A.TEMSILCI_KOD = 9
AND B.BAYI_KOD = 17
AND A.HESAP_NO = B.HESAP_NO)
SET TEMSILCI_KOD = 4
Note: This query will only work in Oracle if (S_TEKLIF.BAYI_KOD, S_TEKLIF.HESAP_NO) is unique (so that the update will not be ambiguous and each row from S_MUSTERI will be updated at most once).

Your update statement does not follow the correct syntax. There is no from clause in the update statement. It should follow the format
Update <table>
set <column> = <value>
where <conditions>
See this documentation on update:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10007.htm#i2067715

Related

Error in SQL statement: ParseException: mismatched input 'FROM' expecting from Databricks SQL

I am trying to update the columns based on other table data but receiving the below error
Trying to execute in Databricks
Error in SQL statement: ParseException:
mismatched input 'FROM' expecting {<EOF>, ';'}(line 8, pos 0)
== SQL ==
UPDATE
rd
SET
rd.transaction_date = olc.transaction_date,
rd.store_number = olc.store_number,
rd.terminal_number = olc.terminal_id,
rd.sequence_number = olc.transaction_number
FROM delta.`/reservation_detail_olc` rd
^^^
UPDATE
rd
SET
rd.transaction_date = olc.transaction_date,
rd.store_number = olc.store_number,
rd.terminal_number = olc.terminal_id,
rd.sequence_number = olc.transaction_number
FROM delta.`reservation_detai` rd
inner JOIN
delta.`order_line` olc
ON rd1.confirmation_number =olc.res_confirmation_number
WHERE rd.confirmation_number =olc.res_confirmation_number
The syntax used in Databricks SQL guide for update command does not have FROM clauses. They do not support it.

Update multiple columns from a sub query

UPDATE PINPOINT_SUPPLEMENT
SET (ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (
SELECT key,'file'
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
)
WHERE ATTACHMENT_VALUE IS NULL
Getting Error when i execute this query
ERROR: syntax error at or near "SELECT"
LINE 3: SELECT key,'file
update PINPOINT_SUPPLEMENT
set
ATTACHMENT_VALUE = PINPOINT_DOCUMENT.key,
ATTACHMENT_TYPE = 'file'
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
or
update PINPOINT_SUPPLEMENT
set
(ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (PINPOINT_DOCUMENT.key, 'file')
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
Support for updating tuples was introduced in Postgres 9.5, so you can't use that syntax with your version.
But as the second value is a constant I don't see a reason to use that syntax to begin with:
UPDATE PINPOINT_SUPPLEMENT
SET ATTACHMENT_VALUE = (SELECT "key"
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID),
ATTACHMENT_TYPE = 'file'
WHERE ATTACHMENT_VALUE IS NULL
Note, that the sub-query might result in an error if there is more than one ATTACHMENT_VALUE for a document_id!

sybase script don't understand trouble

I 'm not a regular of Sybase ASE.
Tring to make a script to add line into a tempory table.
sybase version 12.5.4
BEGIN
declare and init variable
--boucle
select * into #parTemp from Parution where 1=2
WHILE #dateCourante <= #dateFin
BEGIN
select #dpart1=datepart(mm,#dateCourante)
select #dpart2=datepart(dd,#dateCourante)
select #dpart3=datepart(dw,#dateCourante)
--si on est pas le 1er mai
if #dpart1 <> 5 AND #dpart2 <> 1
begin
--id parution
select #idPar = #idPar + 1
select #idParChaine = convert(varchar(10),#idPar)
--num parution
select #numPar = #numPar + 1
select #numParChaine = convert(varchar(20),#numPar)
--prix du jour courant
if datepart(dw,#dateCourante)=6
select #prixCourant = #prixVen
else
if datepart(dw,#dateCourante)=1
select #prixCourant = #prixDim
else
select #prixCourant = #prix
end
end
insert into #parTemp values (#idParChaine,#dateCourante,#numParChaine,#nbPagination,#prixCourant,#poids,#parCompt,#parOJD,#resVal)
end
select #dateCourante = dateadd(dd,1,#dateCourante)
END
END
Errors i get :
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'WHILE'.
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'end.
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'End'.
i really don't understand, thank for help.
Pierre

Postgres syntax error in if condition

I am trying to calculate commission and according to that commission value (commiss) i want to update a column value fixed_fee
select st1.* ,(st1.order_item_value * st1.commission_rate)/100 commiss ,
if commiss < 50 then
update payment_error set fixed_fee = -5 where commiss <= 50
else if commiss >= 50 then
update payment_error set fixed_fee = -10 where commiss >=50
else
end if
from payment_error as st1 join payment_error as st2 on st1.order_item_id=st2.order_item_id ;
while running is then error come is :
ERROR: syntax error at or near "<"
LINE 2: if commiss<50 then
^
********** Error **********
ERROR: syntax error at or near "<"
SQL state: 42601
Character: 87
You can't use if in SQL. This is only available in procedural languages like PL/pgSQL.
But more importantly you cannot embed an update statement inside a select statement like that.
What you are trying to do can be done with a single UPDATE statement using a CASE:
update payment_error
set fixed_fee = case
when (order_item_value * commission_rate)/100 <= 50 then -5
else 5 --<<< different value here
end;
You are setting the fixed_fee to -5 in both cases - I assume that is a typo, and you do mean to assign a different fee for the case when the comission is greater than 50
You cannot use update and select together. To update you can try this:
update payment_error
set fixed_fee = case when (order_item_value * commission_rate * .01) < 50 then -5
else 5 --In your case you have use both as -5 so change it as per your reqm

What's wrong in this delete query?

DELETE CUSTSEGCONTROL.x FROM CUSTSEGCONTROL x, CUSTSEGCONTROL y
WHERE x.custsegcontrolid < y.custsegcontrolid
AND x.customerid = y.customerid
AND tripid='A2G0G5'
AND registrationstatus!=3;
I'm getting this error message.
Error starting at line 1 in command: DELETE x FROM CUSTSEGCONTROL x, CUSTSEGCONTROL y WHERE x.custsegcontrolid < y.custsegcontrolid AND x.customerid = y.customerid AND tripid='A2G0G5' AND registrationstatus!=3
Error at Command Line:1 Column:9
Error report: SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
I think your problem is in this line:
DELETE CUSTSEGCONTROL.x
It seems you are trying to delete column x from CUSTSEGCONTROL, which is invalid.
Try this:
delete custsegcontrol x
where x.customerid in
( select y.customerid
from custsegcontrol y
where x.custsegcontrolid < y.custsegcontrolid
and tripid = 'A2G0G5'
and registrationstatus != 3
)
The following version should work in Oracle. Ironically, this won't work in MySQL:
DELETE FROM CUSTSEGCONTROL x
WHERE EXISTS (SELECT 1
FROM CUSTSEGCONTROL y
WHERE x.custsegcontrolid < y.custsegcontrolid AND
x.customerid = y.customerid AND
tripid = 'A2G0G5' AND
registrationstatus <> 3
);