Sorry if the title is unclear. Basically I'm trying to select certain records from multiple tables then update a certain column value for the returned records.
T-SQL Implementation
UPDATE
CUSTOMERS
SET
LIKES_US = 'Y'
FROM
RESTAURANT REST INNER JOIN CUSTOMERS CUST ON REST.LINK_ID = CUST.LINK_ID
WHERE
REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL
Oracle
UPDATE
(SELECT CUST.LIKES_US
FROM CUSTOMERS CUST INNER JOIN RESTAURANT REST ON CUST.LINK_ID=REST.LINK_ID
WHERE REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL) NEW_CUST
SET
NEW_CUST.LIKES_US = 'Y';
I am tried doing the same thing in Teradata as I did in Oracle but I get the following error:
Executed as Single statement. Failed [3707 : 42000] Syntax error, expected something like a name or a Unicode delimited identifier or an 'UDFCALLNAME' keyword between the 'UPDATE' keyword and '('.
Elapsed time = 00:00:00.003
STATEMENT 1: Unknown failed.
I looked online for the solution but had no luck.
Have you tried the following syntax with Teradata:
UPDATE CUSTOMERS C1
FROM (SELECT C2.LINK_ID
FROM CUSTOMERS C2
INNER JOIN RESTAURANTS R2
ON C2.LINK_ID = R2.LINK_ID
WHERE R2.REST_TYPE = 'DINER'
AND C2.LIKES_US IS NULL) D1
SET LIKES_US = 'Y'
WHERE C1.LINK_ID = C2.LINK_ID
I think that in this specific case, the below query will perform a little better since it needs one less join.
UPDATE C
FROM CUSTOMERS C, RESTAURANTS R
SET LIKES_US = 'Y'
WHERE
C.LINK_ID = R.LINK_ID
AND R.REST_TYPE = 'DINER'
AND C.LIKES_US IS NULL
Related
I saw this SQL Update statement in a trigger and am unsure if the update works accurately - based on looking at where the table alias is and the update table syntax.
The syntax doesn't give any error on execution, and updates the record correctly when executing on random samples on my test DB.
However, on a larger PROD DB with more records, is there a possibility that the update fails or skips altogether? There were reports that random records did not have the SAMPLE.ISCOMPOSITESAMPLE field set.
Questionable syntax
UPDATE SAMPLE SET
SAMPLE.SAMPLETYPE = (SELECT DESCRIPTION FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO),
SAMPLE.ISCOMPOSITESAMPLE = (SELECT COMPOSITESAMPLE FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO)
FROM SAMPLE C
INNER JOIN INSERTED T ON C.SAMPLENO = T.SAMPLENO
Syntax I am familiar with (similar to above but intentionally not optimised for comparison)
UPDATE C SET
SAMPLETYPE = (SELECT DESCRIPTION FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO),
ISCOMPOSITESAMPLE = (SELECT COMPOSITESAMPLE FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO)
FROM SAMPLE C
INNER JOIN INSERTED T ON C.SAMPLENO = T.SAMPLENO
The query is correct and will work. However, there are two things that I would fix:
TheUPDATE SAMPLE does update the table whose alias is C. This is documented as correct and something that really irks me, because aliases should be respected. You should use the alias for the update.
The correlated subqueries are not using fully qualified column names.
So, using correlated subqueries, I would recommend:
UPDATE S
SET SAMPLETYPE = (SELECT ST.DESCRIPTION FROM SAMPLETYPE ST WHERE ST.SAMPLETYPENO = S.SAMPLETYPENO),
ISCOMPOSITESAMPLE = (SELECT ST.COMPOSITESAMPLE FROM SAMPLETYPE ST WHERE ST.SAMPLETYPENO = S.SAMPLETYPENO)
FROM SAMPLE S INNER JOIN
INSERTED I
ON S.SAMPLENO = I.SAMPLENO ;
You can also write this -- probably more efficiently -- using a LEFT JOIN:
UPDATE S
SET SAMPLETYPE = ST.DESCRIPTION,
ISCOMPOSITESAMPLE = ST.COMPOSITESAMPLE
FROM SAMPLE S INNER JOIN
INSERTED I
ON S.SAMPLENO = I.SAMPLENO LEFT JOIN
SAMPLETYPE ST
ON ST.SAMPLETYPENO = S.SAMPLETYPENO;
I'm trying to update a column in a table that makes a join on itself to filter out data.
Initially, the piece of code was for SQL Server and I tried to change it so it run in Vertica. I'm getting this error:
ERROR: Syntax error at or near "inner"
update REPORT.sub_2018_ALL a
inner join REPORT.sub_2018_ALL p
on a.MBR_ID= p.MBR_ID and a.NAME = p.NAME
set RESULT = 'F'
where p.STATUS_REASON = 'Submitted' and a.REVIEW_RESULT is null
I'm not sure if it's because of the alias or if it's not possible to update when a table joins on itself. Any help is appreciated.
I think you want something like this:
update REPORT.sub_2018_ALL a
set RESULT = 'F'
where a.REVIEW_RESULT is null and
exists (select 1
from REPORT.sub_2018_ALL p
where a.MBR_ID = p.MBR_ID and
a.NAME = p.NAME and
p.STATUS_REASON = 'Submitted'
);
For the record, your original syntax would not work on SQL Server, although it might work on MySQL.
I have three tables(A, B, and C) and would like to update null values in A.appointment_id with values in C.tc_appointment_id. Table A and C can be joined via table B. Blue arrows represent joins, the red arrow represents the update I am trying to achieve.
I am able to join the three tables together and have tried to modify my select statement into an update. My successful select statement and unsuccessful update follow.
--Working select
select A.tc_ASN_id,A.appointment_id, B.appt_obj_id, B.appointment_id, C.appointment_id, C.tc_appointment_id from B
join A on B.appt_obj_id = A.asn_id
join C on C.appointment_id = B.appointment_id
where C.appt_status < '9' and A.appointment_id is null;
--Update attempt that ends with SQL Error: ORA-00933: SQL command not properly ended
update asn set appoinmtent_id = ilm_appointments.tc_appointment_id
join ilm_appointment_objects on ilm_appointment_objects.appt_obj_id = asn.asn_id
join ilm_appointments on ilm_appointments.appointment_id = ilm_appointment_objects.appointment_id
where ilm_appointments.appt_status < '9' and asn.appointment_id is null;
The expected result is to update all null values for A.appointment_id to be updated with the values from C.tc_appointment_id.
UPDATE...JOIN syntax is not supported in Oracle. You could use a correlated subquery instead.
Consider:
UPDATE A
SET A.appointment_id = (
SELECT C.tc_appointment_id
FROM B
INNER JOIN C ON C.appointment_id = B.appointment_id
WHERE B.appt_obj_id = A.asn_id
)
WHERE A.appointment_id IS NULL;
Beware that the subquery must return a unique record, otherwise you will get an error like "subquery returned more than one row". Given your sample data this seems to be the OK.
I'm trying to put together a query that updates a field within a table. I'm attempting to run a sub select query that gives me a number, and then use that number that resulted from the sub-query as part of the criteria for the update query.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =[tbl_Foundation_Account].[Client_ID]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID =
[2_Import_tbl_AWCDSU].[ECPD Profile ID]))
My issue is I keep receiving this error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
Am I using the sub-query incorrectly? When I've received this error before, it's been because of some ambiguity in the table or field names, but this time I've checked for all that and it should be fine. Can anyone explain what SQL sin I have committed?
On the error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
This is because the table column [tbl_Foundation_Account].[Client_ID] does not exists in the scope of outer UPDATEquery .
The only table the outer query has an inkling about is [2_import_VZW_tbl_SMTN] and it does not have a column like [tbl_Foundation_Account].[Client_ID].
It is akin to writing a column name with a typo or like you said
When I've received this error before, it's been because of some
ambiguity in the table or field names
Please try a query like below.
Note that I am using Inner query syntax and ensuring that a single value is returned by using
select top 1 [Client_ID]
in the inner query. rest of the query syntax is same.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =
(
select top 1 [Client_ID]
from [tbl_Foundation_Account]
where [Foundation_Account_ID] =
(
Select TOP 1 a.Foundation_Account_ID
FROM tbl_Foundation_Account a
INNER JOIN [2_Import_tbl_AWCDSU] b
ON a.Foundation_Account_ID = b.[ECPD Profile ID]
)
)
Another poster submitted this answer earlier, but then deleted it. I was able to try it before they deleted it and it works exactly how I needed it to work. I will use this as the right answer unless someone else can tell me why this is a bad Idea.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] = [tbl_Foundation_Account].[Client_ID]
from [tbl_Foundation_Account]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID = [2_Import_tbl_AWCDSU].[ECPD Profile ID]))
I get an error Invalid column name 'BookingDate' for the following query. The column is there. What is wrong?
BEGIN TRANSACTION updateBookingFeeByFeeTypeId
UPDATE b
SET b.StandardFee = 22
FROM
Core.Booking b
INNER JOIN Core.Fee f
ON f.FeeId = b.FeeId
INNER JOIN Core.FeeSchedule fs
ON fs.FeeId = f.FeeId
WHERE FeeScheduleId = 8193
AND b.BookingDate >= '15-jul-2014'
AND StandardFee <> 22
ROLLBACK TRANSACTION updateBookingFeeByFeeTypeId
COMMIT TRANSACTION updateBookingFeeByFeeTypeId
What's wrong is that I made a faulty assumption. The column doesn't exist. I made this assumption after referencing a complex view that's confusing, and makes it look like the booking table contains this column when it actually doesn't