I have an SQL query which keeps giving me an error.
I have tried multiple ways of writing the query but I have had no luck in fixing it.
I have two tables(table1 and table2) with duplicate columns orgcodeold and orgcode. table1.orgcode is empty but table2.orgcode is populated.
I am trying to populate table1.orgcode with table2.orgcode where table1.orgcodeold=table2.orgcodeold.
THE ERROR
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'.
THE QUERY
UPDATE table1 AS t1
LEFT JOIN table2 AS t2
ON t2.orgcodeold = t1.orgcodeold
SET t1.orgcode = t2.orgcode
WHERE t1.orgcodeold = t2.orgcodeold
Please help.
Well, you have almost the whole syntax wrong. It should be:
UPDATE t1
SET t1.orgcode = t2.orgcode
FROM table1 AS t1
INNER JOIN table2 AS t2
ON t2.orgcodeold = t1.orgcodeold;
This should work:
UPDATE t1
SET t1.orgcode = t2.orgcode
from table1 AS t1
LEFT JOIN table2 AS t2
ON t2.orgcodeold = t1.orgcodeold
Related
I tried to delete an entry from table1 based on criteria on table2. (id in table1 is foreign key from table2)
I tried all those below and all returned with syntax errors.
take 1:
delete table1.* from table1 inner join table2 on table1.id=table2.id where table2.column3=21 and table2.column4=59;
Error: near "table1": syntax error
take 2:
delete table1 from table1 inner join table2 on table1.id=table2.id where table2.column3=21 and table2.column4=59;
Error: near "table1": syntax error
take 3:
delete from table1 inner join table2 on table1.id=table2.id where table2.column3=21 and table2.column4=59;
Error: near "inner": syntax error
Anyone knows what are the correct syntax?
If this is the wrong approach, what is the correct way to achieve my goal?
Really appreciate it.
No Join in SqlLite DELETE, by the look of it, but you could use exists:
delete
from table1
where exists
(select 1
from table2
where table1.id=table2.id
and table2.column3=21
and table2.column4=59
);
Since SQLite does not support JOIN yet in outer query of DELETE statements, consider using subquery with IN or EXISTS:
DELETE FROM table1
WHERE id IN (
SELECT id
FROM table2
WHERE table2.column3 = 21
AND table2.column4 = 59
)
DELETE FROM table1
WHERE EXISTS (
SELECT 1
FROM table2
WHERE table1.id = table.id
AND table2.column3 = 21
AND table2.column4 = 59
)
Former SQL Server dataminer here expanding my skills. Complete newbie to Oracle. I've run into multiple error messages trying to convert this SQL query to work in Oracle:
UPDATE table1
SET program = SUBSTR(table2.project,1,5)
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table1.program = 'THE_PROGRAM_NAME'
AND table2.program = 'THE_PROGRAM_NAME';
I've dealt with each error having gone through various StackOverflow questions, but none I've encountered have helped me resolve it fully. The basic problem is that Oracle doesn't want to deal with taking one line from the joined table to update multiple lines from the primary table, and I don't know how to address this.
All I've read so far seems to indicate that this is an insurmountable problem so I'm asking my own new question to have that confirmed or refuted, and either get a whole new approach to try or that so-far-elusive solution.
This is as far as I have gotten, and it's led me to "ORA-30926 unable to get a stable set of rows in the source tables":
MERGE INTO table1 ce
USING
(SELECT DISTINCT SUBSTR(table2.project,1,5) newvalue, table1.code, table1.id
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table1.program = 'THE_PROGRAM_NAME'
AND table2.program = 'THE_PROGRAM_NAME'
) combined
ON (ce.id = combined.id
AND ce.code = combined.code)
WHEN MATCHED THEN
UPDATE SET ce.program = combined.newvalue;
If you need more information to be able help please ask.
Any help greatly appreciated.
Regards,
Oracle does not support joins in update queries. A typical translation would use a correlated subquery:
UPDATE table1 t1
SET program = (
SELECT SUBSTR(t2.project,1,5)
FROM table2 t2
WHERE t1.id = t2.id AND t2.program = t1.program
)
WHERE program = 'THE_PROGRAM_NAME';
Note that this would update program to null if there is no match in table2. If you want to avoid that, then add a condition in the WHERE clause:
UPDATE table1 t1
SET program = (
SELECT SUBSTR(t2.project,1,5)
FROM table2 t2
WHERE t1.id = t2.id AND t2.program = t1.program
)
WHERE program = 'THE_PROGRAM_NAME' AND EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.id = t2.id AND t2.program = t1.program
);
I have the following SQL code (this is how much I've got so far):
MERGE INTO SCHEMA1.TABLE_1 table1 USING
(
SELECT DISTINCT table2.column1,
view1.column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
) t2 ON (table1.column3 = t2.column1 )
WHEN MATCHED THEN
UPDATE
SET table1.column4 = t2.column2;
The following is the definition of VIEW_1 :
CREATE VIEW SCHEMA_2.VIEW_1
AS (SELECT
SCHEMA_2.TABLE_1.COLUMN_1,
SCHEMA_2.TABLE_2.COLUMN_1,
SCHEMA_2.TABLE_2.COLUMN_2,
SCHEMA_2.TABLE_2.COLUMN_3,
SCHEMA_2.TABLE_5.COLUMN_1,
SCHEMA_2.TABLE_6.COLUMN_1,
SCHEMA_2.TABLE_6.COLUMN_2,
SCHEMA_2.TABLE_6.COLUMN_3,
SCHEMA_2.TABLE_6.COLUMN_4,
SCHEMA_2.TABLE_7.COLUMN_1,
SCHEMA_2.TABLE_7.COLUMN_2,
SCHEMA_2.TABLE_8.COLUMN_1
FROM SCHEMA_2.TABLE_1
INNER JOIN SCHEMA_2.TABLE_2
ON SCHEMA_2.TABLE_1.COLUMN_1 = SCHEMA_2.TABLE_2.COLUMN_2
INNER JOIN SCHEMA_2.TABLE_5
ON SCHEMA_2.TABLE_1.COLUMN_4 = SCHEMA_2.TABLE_5.COLUMN_3
LEFT OUTER JOIN SCHEMA_2.TABLE_6
ON SCHEMA_2.TABLE_2.COLUMN_2 = SCHEMA_2.TABLE_6.COLUMN_4
LEFT OUTER JOIN SCHEMA_2.TABLE_7
ON SCHEMA_2.TABLE_2.COLUMN_1 = SCHEMA_2.TABLE_8.COLUMN_5
);
But I'm getting the below error message:
Error report -
SQL Error: ORA-30926: unable to get a stable set of rows in the source tables
30926. 00000 - "unable to get a stable set of rows in the source tables"
*Cause: A stable set of rows could not be got because of large dml
What causes the error? Where to change in the code to make it work?
Thanks for helping out!
For this example your problem is definitely in the USING subquery. This query produces more than one value of table2.column1:
SELECT DISTINCT table2.column1,
view1.column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
So the ON clause will match the same row(s) in table1 more than once:
ON (table1.column3 = t2.column1 )
Oracle cannot figure out which value of t2.column2 should be used in the UPDATE, so it hurls ORA-30926.
Using distinct in the subquery doesn't help because that gives permutations of all the columns. You need to write a subquery which will produce unique values of t2.column1 across all rows, or add another identifying column(s) to generate a unique key you can join to table1.
In my experience, this error is returned, not only when the USING clause returns more than one row for a row in the MATCH table, but also frequently when it cannot be sure that only one row will be returned (even if there are no actual cases of multiple rows being returned). To force the parser to accept the query in cases like this, I usually resort to using a GROUP BY on the MATCH..ON column(s).
MERGE INTO SCHEMA1.TABLE_1 table1 USING
(
SELECT table2.column1,
MAX(view1.column2) as column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
GROUP BY table2.column1
) t2 ON (table1.column3 = t2.column1 )
WHEN MATCHED THEN
UPDATE
SET table1.column4 = t2.column2;
Not sure why its throwing an error at t2. I am trying to run a simple sql query.
Running on MS-SQL and the error message says 'incorrect syntax near t2'
UPDATE t1
SET t1.EmpSubCompetency = t2.EmpSubCompetency,
t1.Competency = t2.Competency,
t1.FileName = t2.FileName,
t1.Longitude = t2.Longitude,
t1.Latitude = t2.Latitude,
t1.SubAreaName = t2.Region,
t1.SectorTag=t2.SectorTagClassification
FROM dbo.STG_MyCompetencies t1
LEFT JOIN (select * from dbo.STG_EmployeeMaster where Act_Flg='Y') t2
Your problem is the missing ON clause. Further, you don't need a subquery for this logic:
FROM dbo.STG_MyCompetencies t1 LEFT JOIN
dbo.STG_EmployeeMaster t2
ON t1.??? = t2.??? AND
em.Act_Flg = 'Y'
Note that unmatched rows will have all the columns set to NULL.
The ??? is for whatever column should be used for the JOIN.
I have this query, which returns all records from Keywords which does not have a Quote
The query is working, but I want to DELETE all these records, the problem is that if I put DELETE instead of SELECT I get errors.
SELECT Keywords.[Id]
,[QuoteId]
FROM [QuotesTemple].[dbo].[Keywords]
LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
WHERE Quotes.Id IS NULL
This does not work.
DELETE
FROM [QuotesTemple].[dbo].[Keywords]
LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
WHERE Quotes.Id IS NULL
I get this error:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'LEFT'.
Write it like this:
DELETE [QuotesTemple].[dbo].[Keywords] FROM [QuotesTemple].[dbo].[Keywords]
LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
WHERE Quotes.Id IS NULL
Valid syntax is:
DELETE [QuotesTemple].[dbo].[Keywords]
FROM [QuotesTemple].[dbo].[Keywords] AS k
LEFT JOIN [QuotesTemple].[dbo].[Quotes] AS q ON k.QuoteId = q.Id
WHERE q.Id IS NULL
you can also write as below
delete t1 FROM projects AS t1 LEFT OUTER JOIN [QuotesTemple].[dbo].Quotes AS t2 on t1.QuoteId= t2.QuoteId where t2.Id is Null