Update table1 t1
set t1. col1='op'
where t1.col2 in (select t2.col2 from table2 t2 where t1.col3 = t2.col3) ;
The above query is working in Oracle but not in SQL server.
Throwing error: incorrect syntax near t1.
You can't set an alias in the UPDATE clause in SQL Server. The correct syntax would be to drop the alias or alias the object in the FROM:
--Without Alias
UPDATE table1
SET col1 = 'op'
WHERE EXISTS (SELECT 1
FROM Table2 AS T2
WHERE T2.col3 = table1.col3
AND T2.col2 = table1.col2);
--FROM and JOIN
UPDATE T1
SET col1 = 'op'
FROM Table1 AS T1
JOIN Table2 AS T2 ON T1.Col2 = T2.Col2
AND T1.Col3 = T2.Col3;
--With Aliases
UPDATE T1
SET col1 = 'op'
FROM Table1 AS T1
WHERE EXISTS (SELECT 1
FROM Table2 AS T2
WHERE T2.col3 = table1.col3
AND T2.col2 = table1.col2);
Related
I am trying to use the below query, its everytime saying cannot insert null for the second column.
UPDATE TABLE1
SET (COL1,COL2,COL3) = (SELECT COL1,COL2,COL3
FROM TABLE2
WHERE t1.id = t2.id);
Try this:
UPDATE TABLE1 SET (COL1,COL2,COL3) =
(SELECT COL1,NVL(COL2,0),COL3 FROM TABLE1 T1, TABLE2 T2 where t1.id = t2.id)
WHERE <YOUR WHERE CONDITION>
I would use a MERGE:
MERGE INTO TABLE1 t1
USING (SELECT id, COL1,COL2,COL3
FROM TABLE2) t2
ON (t1.id = t2.id)
WHEN MATCHED THEN UPDATE SET t1.COL1 = t2.COL1
, t1.COL2 = t2.COL2
, t1.COL3 = t2.COL3;
There are rows in table1 where not corresponding row exists in table2 thus the sub-query returns nothing which means the UPDATE statement will take that as null for all three columns.
You have to add a WHERE clause that only updates rows in table1 that do have a matching row in table2
UPDATE TABLE1
SET (COL1,COL2,COL3) = (SELECT COL1,COL2,COL3
FROM TABLE2 t2
WHERE table1.id = t2.id)
where exist (select *
from table2 t2
where table1.id = t2.id);
Am trying to execute the below update statement in H2
UPDATE table1
SET STATUS_CD = 1
WHERE (col1, col2)
IN (select col1, col2 FROM table2)
but it produces Subquery is not a single column query
Use Exists
UPDATE table1 t1
SET STATUS_CD = 1
WHERE exists (Select 1 FROM table2 t2 where t1.col1 = t2.col1 and t1.col2 = t2.col2)
I am working on SQL 2008. I have two identical tables with same column names.
On Table2, i am missing some records. Some records got deleted in the Table2.
I have to compare Table1 and Table2 and retrieve only missing records from table1.
Use a LEFT JOIN and check for IS NULL like below. where t2.col2 is null will be TRUE only when there are records in table1 which are not present in table2. Which is what you are looking for. [This is a sample code and have no resemblance with your original query]
select t1.col1,t1.col2,t1.col3
from table1 t1
left join table2 t2 on t1.some_column = t2.some_column
where t2.col2 is null
You should use SQL Except. There is no Join involved.
Select * from dbo.TableA
Except
Select * from dbo.TableB
In set theory, the difference of sets A, B (A-B) is the set of elements that belong to A and do not belong to B.
With an " not exists", you have a solution :
select * from Table1 t1 where not exists (select 1 from Table2 t2
where t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3
and ... // check here all columns ...
)
There is however a little problem in this solution in the case of null values, which can only be tested via a "IS NOT NULL" or "IS NULL", hence the complementary solution:
select * from Table1 t1 where not exists (select 1 from Table2 t2
where (t1.col1 = t2.col1 or (t1.col1 IS NULL and t2.col1 IS NULL))
and (t1.col2 = t2.col2 or (t1.col2 IS NULL and t2.col2 IS NULL))
and (t1.col3 = t2.col3 or (t1.col3 IS NULL and t2.col3 IS NULL))
and ... // check here all columns ...
)
I have three tables Table1, Table2 and Table3 and the following query which deletes rows in Table1
delete from Table1
where EXISTS
(select (1) from Table2
where Table1.col1=Table2.col1
AND Table1.col2=Table2.col2
AND Table1.col3=(select **Table3.col3 from Table3** inner join Table2 on Table3.col1=Table2.col1)
Is this query correct? If not, how to use a third table inside the where condition?
Edit : Also, please explain how to rewrite the query if we want to delete the rows from table2 which itself is joined with table3?
Here's one way to do it:
delete from table1
where (col1, col2, col3) in (
select t1.col1, t1.col2, t1.col3
from table1 t1
join table2 t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2
join table3 t3 on t1.col3 = t3.col3 and t2.col1 = t3.col1
);
SQL Fiddle Demo
Or using EXISTS might be faster:
delete table1
where exists (
select *
from table2
join table3 on table2.col1 = table3.col1
where table1.col3 = table3.col3 and
table1.col1 = table2.col1 and
table1.col2 = table2.col2);
I have 3 fields per row in table 1 that I want to compare to the exact same fields per row in table 2
I have been playing around with NOT IN, but I am not having any luck. Can anyone help?
Basically I want to see all records from both tables where field 1, field 2, field 3 don't match in table 2
How would this be written?
SELECT *
FROM Table1 T1
FULL OUTER JOIN Table2 T2
ON T1.col1 = T2.col1
AND T1.col2 = T2.col2
AND T1.col3 = T2.col3
WHERE T1.col1 <> T2.col1
OR T1.col2 <> T2.col2
OR T1.col3 <> T2.col3
jyparask's answer is close, but it may not work if some of your columns are nullable and the where clause is off a bit. In that case you need to either coalesce the columns to a value they couldn't actually contain, or check for nulls manually.
I'm going to assume col1 is not nullable, and cols 2 and 3 are
SELECT *
FROM Table1 T1
FULL OUTER JOIN Table2 T2
ON T1.col1 = T2.col1
AND (T1.col2 = T2.col2 or (T1.col2 is null and T2.col2 is null))
AND (T1.col3 = T2.col3 or (T1.col3 is null and T2.col3 is null))
WHERE T1.col1 is null or T2.col1 is null # assuming col1 is not null
Or with a coalesce:
SELECT *
FROM Table1 T1
FULL OUTER JOIN Table2 T2
ON T1.col1 = T2.col1
AND COALESCE(T1.col2,-1) = COALESCE(T2.col2,-1)
AND COALESCE(T1.col3,-1) = COALESCE(T2.col3,-1)
WHERE T1.col1 is null or T2.col1 is null # assuming col1 is not null