SQL Update syntax check - sql

Writing a simple update statement in Teradata and I'm having trouble getting it to work. I'm getting a syntax error saying: Syntax error: expected something between the word 'First_name' and the 'FROM' keyword . This is reference to line 7. I have no idea what I'm missing.
Here's the code with some redacted object names:
UPDATE DATA.CONTACTS tgt
SET
tgt.LAST_NAME = TABLES.PART.LAST_NAME
,tgt.BPP_USER_ID = TABLES.PART.User_Id
,tgt.Email_Address = TABLES.PART.Email
,tgt.Last_name = TABLES.PART.Last_name
,tgt.First_name = TABLES.PART.First_name
FROM
(SELECT
C_C
, USER_ID
, Email
,Last_name
,First_name
FROM TABLES.PART) --ppage
WHERE EMAIL_ADDRESS IN (
SELECT Email
FROM DATA.CONTACTS
);
select * from mmbi_tables_data.crm_mmbi_contacts
I've tried deleting using that ppage alias but I still get the same error regardless of what I do.

This doesn't look so simple. I think the following will work in Teradata:
UPDATE tgt
FROM data.contacts tgt, tables.part ppage
SET LAST_NAME = TABLES.PART.LAST_NAME,
BPP_USER_ID = TABLES.PART.User_Id,
Email_Address = TABLES.PART.Email,
Last_name = TABLES.PART.Last_name,
First_name = TABLES.PART.First_name
WHERE tgt.email = ppage.email_address;

Related

SQL Is it possible to incorporate a SELECT with a REPLACE?

I'm using MS SQL Server 2019
I have a string in a table (tblJobCosts) that has its own ID like this:
TextID jobText
1 Total Cost for job is £[]. This includes VAT
How do I update the value stored in the brackets based on the value from another table?
The end result would look like this:
Total Cost for job is £500. This includes VAT
I thought I could incorporate a SELECT with a REPLACE but this does not seem possible:
DECLARE #JobNum INT = 123;
UPDATE dbo.JobCosts
SET jobText = REPLACE (jobText,'[]',
SELECT JH.jobCost
FROM dbo.JobHead AS JH
WHERE (JH.JobNo = #JobNum)
) AND TextID = 1
If I run the above I receive the error:
Incorrect syntax near the keyword 'SELECT'.
Is it possible to incorporate a SELECT with a REPLACE?
I think that you cannot call a select statement in the replace function.
I would try something like that:
UPDATE dbo.JobCosts
SET jobText = REPLACE (jobText,'[]',k.the_cost) from
( SELECT JH.jobCost as the_cost
FROM dbo.JobHead AS JH
WHERE (JH.JobNo = #JobNum)
)k
where TextID = 1

how to use update query in to update query in postgresql

I want to update ChannelInfo what ever Reporting ChannelId update row returns
but this query is not working, giving error:
ERROR: syntax error at or near ""AdsReporting""
LINE : where "CampaignInfo"."id" in (update "AdsReporting" set "adS...
^
SQL state: 42601
Character: 240
can anyone please help me?
Reporting ChannelId is a foreign key relationship with ChannelInfo primary key.
code:
update "ChannelInfo" set "Amount"=CASE WHEN "Duration"='60' THEN "Amount"-12.25 ELSE "Amount"-6.13 END
where "id"=(update "Reporting" set "Status"='run' where "Status"='a' RETURNING "ChannelId");
It sounds like you want to use CTEs:
with u as (
update "Reporting"
set "Status" = 'run'
where "Status" = 'a'
RETURNING "ChannelId"
)
update "ChannelInfo" ci
set "Amount"= (CASE WHEN ci."Duration" = '60' THEN ci."Amount" - uc.cnt * 12.25 ELSE ci."Amount" - uc.cnt * 6.13 END)
from (select "ChannelId", count(*) as cnt
from users u
group by "ChannelId"
) uc
where uc."ChannelId" = ci.id;

GETTING ERROR-- ORA-00936:MISSING EXPRESSION for below query please help on this

SELECT CASE (SELECT Count(1)
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(
SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988')
)
WHEN 0 THEN
(
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27' )
WHEN 1 THEN
(
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
ELSE NULL
END
FROM dual;
You need to recreate your query and make sure to follow the flow of the clauses properly, please check the next two links to get a better understanding :
[ORA-00936: missing expression tips]
How do I address this ORA-00936 error?
Answer: The Oracle oerr utility notes this about the ORA-00936 error:
ORA-00936 missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
The ORA-00936 happens most frequently:
1 - When you forget list of the column names in your SELECT statement.
2. When you omit the FROM clause of the SQL statement.
ora-00936-missing-expression
I hope this can help you.
You cannot use a simple select query like this. You have to use a PL/SQL block like below -
DECLARE NUM_CNT NUMBER := 0;
BEGIN
SELECT Count(1)
INTO NUM_CNT
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988');
IF NUM_CNT = 0 THEN
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27';
ELSIF NUM_CNT = 1 THEN
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
END IF;
END;

Oracle PLSQL using a dynamic variable in a where clause

For testing in Toad, I have the following code
select ...
from ...
where term_code = :termcode AND
(
case
when :theSubject is not null then SUBJ_CODE = :theSubject
else 1 = 1
end
)
AND ptrm_code <> 8
In short: If theSubject is not entered (is null) I want to display all the courses, otherwise I want to display only those where subject_code is the same as the one entered in the variable window in Toad.
But I get an error:
[Error] Execution (77: 68): ORA-00905: missing keyword
in here:
when :theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse
You can use boolean logic:
where
term_code = :termcode
and (:theSubject is null or subj_code = :theSubject)

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!