Sql server update multiple columns from another table - sql

I have read lots of post about how to update multiple columns but still can't find right answer.
I have one table and I would like update this table from another table.
Update table1
set (a,b,c,d,e,f,g,h,i,j,k)=(t2.a,t2.b,t2.c,t2.d,t2.e,t2.f,t2.g,t2.h,t2.i,t2.j,t2.k)
from
(
SELECT ..... with join ... where ....
) t2
where table1.id=table2.id
If I running only select statement (between brackets) then script return values but not working with update

TSQL does not support row-value constructor. Use this instead:
UPDATE table1
SET a = t2.a,
b = t2.b,
(...)
FROM
(
SELECT ..... with join ... WHERE ....
) t2
WHERE table1.id = table2.id

You don't need to use a sub-query you can also simply do the following....
Update t1
set t1.a = t2.a
,t1.b = t2.b
,t1.c = t2.c
,t1.d = t2.d
.......
from table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE .......

The above solution will work only for MSSQL.
In case of MySql you just need to first declare the tables
UPDATE
table1 t1 ,table2 t2
set
t1.field=t2.field
where
t1.id=t2.id;
In my case this worked..!!

The UPDATE SET commands implicitly apply on the table specified by , and it is not possible to specify the table on the SET operation.
Edit: Specify only the column name you want to update, do not mention the table.

Related

Update with join in Oracle

I've go this request in SQL for a migration:
UPDATE table1
SET table1.value = table2.value
FROM table1
INNER JOIN table2
ON table2.othervalue2 = table1.key
WHERE table2.othervalue3 is NULL
Works well.
But not with Oracle. I know it isn't impossible to make join in a update with oracle, so I use this:
UPDATE table1 SET table1.value = (SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL)
WHERE EXISTS (SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL)
but I have an ORA-01427: Subquery returns more than one row.
Thanks
i would say you have inconsistent data. From the Errors you get i would say you have duplicates in the query specified.
SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL
you might wanne check it.or you need to have the data better specified you want to select for Update/Merge.

SQL how to insert from another table based on same row values?

I have two tables with similar column values for columns "name" and "regis". One table is already filled with values, the other has only values for "name". How do i get "regis" values from table1 into table2 based on same column values?
I tried something like this
INSERT INTO table2 (regis)
SELECT table1 (regis)
WHERE table2.name = table1.name
Please try:
update table2 set regis = (select regis from table1 where table1.name=table2.name);
see the whole sample on:
https://www.db-fiddle.com/f/3jC8PGeZZEuty3XVq8gdzz/9
I think, what you are after is insert when name not found in table2 or update if its found in both tables.
INSERT INTO TABLE2 (regis)
SELECT t1.REGIS FROM TABLE1 as t1
LEFT OUTER JOIN TABLE2 as t2 on t2.name = t1.name
WHERE t2.name is null
You can update table2 using the statement like below:
update t2
set t2.regis = t1.regis
from table2 as t2
inner join table1 as t1 on t1.name = t2.name
If you are using SQL Server, then you can use merge statement to either insert or update in one step.
You need to use table2 as join in your select statement.
INSERT INTO table2 (regis)
SELECT table1.regis FROM Table1 INNER JOIN Table2 on table1.name = table2.name

How can we perform conditional cross join on 1=1 in sql query?

Initially, I have a query like below, doing a join on 1=1. (It's simply doing a cross join, which selects all rows from the first table and all rows from the second table and shows as a cartesian product, i.e. with all possibilities.)
SELECT * FROM Table1 t1
JOIN Table2 t2 ON 1=1
Problem: Optimize this query in such a way, it will show only the records for a particular ID and if we don't have an ID or have a NULL in the ID then it will show the result same as previously(1=1). So I wrote the script below.
Declare #T2id as int;
Set #T2id = 123;
SELECT * FROM Table1 t1
JOIN Table2 t2 ON
-- left side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
t2.Id
END
=
-- right side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
#T2id
END
Can anyone confirm, is it good or we can have a better approach than this?
I think your way of presenting a cross-join is something I haven't seen before.
My view is it's simpler to read and understand if you just:
SELECT *
FROM Table1 t1, Table2 t2
As for the question, assuming SQL Server (you didn't tag the RDBMS, but I guess from your variable declaration) you might consider:
IF ISNULL(#T2id,1) = 1
SELECT *
FROM Table1 t1, Table2 t2;
ELSE
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id
WHERE t2.id = #T2id;

sql oracle update

I have this SQL...
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2
where t1.id = t2.cpbezeichnung)
where t1.id = t2.cpbezeichnung
... which I cant run because it says me that it doesnt know t2.cpbezeichnung in line #5.
How can I fix it?
Table with alias t2 is not defined for UPDATE query, so it's clearly not known at line 5. Table t2 is defined only inside subquery on lines 3 and 4.
What exactly are you trying to achieve with condition on line 5?
If do you want prevent setting NULL into t1.wert for rows where there is no appropriate record in table t2, then you need to replace condition on line 5
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2 where t1.id = t2.cpbezeichnung)
where t1.id IN (SELECT t2.cpbezeichnung from table2)
This will set values in t1.wert only for records where t1.id exists in t2.cpbezeichnung.
The t2 alias (along with table2) is only visible in the subquery. My guess is that you want
UPDATE table1 t1
SET t1.wert = (select t2.bezeichnung
from table2 t2
where t1.id = t2.cpbezeichnung)
where exists (select 1
from table2 t2
where t1.id = t2.cpbezeichnung)
which updates every row where there is a match between the two tables. If that's not what you want, posting a test case would be helpful.
When using correlated subqueries you cannot use an alias from the inner query in the outer query.
The outer query knows nothing about the inner query except its results.
Ref
You will have to use a Inner join on the two tables.
Something like
UPDATE table1 t1 INNERJOIN table t2 ON your_table_condition SET t1.wert = (select t2.bezeichnung from t2 where t1.id = t2.cpbezeichnung) where t1.id = t2.cpbezeichnung

Using subquery in an update always requires subquery in a where clause?

This is a common SQL query for me:
update table1 set col1 = (select col1 from table2 where table1.ID = table2.ID)
where exists (select 1 from table2 where table1.ID = table2.ID)
Is there any way to avoid having two nearly identical subqueries? This query is an obvious simplification but performance suffers and query is needlessly messy to read.
Unfortunately Informix don't support the FROM clause at UPDATE statement.
The way to workaround and you will get better results (performance) is change the UPDATE to MERGE statement.
This will work only if your database is version 11.50 or above
MERGE INTO table1 as t1
USING table2 as t2
ON t1.ID = t2.ID
WHEN MATCHED THEN UPDATE set (t1.col1, t1.col2) = (t2.col1, t2.col2);
Check IBM Informix manual for more information
Update with inner join can be used to avoid subqueries
something like this:
update t1
set col1 = t2.col1
from table1 t1
inner join table2 t2
on t1.ID = t2.ID
try this:
update table1 set col1 = (select col1 as newcol from table2 where table1.ID = table2.ID)
where exists (newcol)