sql oracle using update - sql

I need to take data from one table to add to another table.
I got two SQL's:
SQL1:
select * from table1;
SQL2:
select * from table2 where coloumn1 = table1.coloumn6 (number)
table1 looks like:
For better understanding we call each coloumn coloumn1, coloumn2, ... coloumn9
table2 looks like:
For better understanding we call each coloumn coloumn1, coloumn2, ... coloumn13
What should happen in text
My SQL has to take the value from table1.coloumn6 (number) - checking if this value is given in table2.coloumn1
select * from table2 where coloumn1 = table1.coloumn6 (SQL2)
If yes it should update the data from table1.coloumn2 (varchar2) to table2.coloumn4 (varchar2).

If I understand correctly, you want to update table1.column2 to the corresponding value in table2.column1 based on other conditions. Based on your description:
update table1
column2 = (select column4 from table2 t2 where t2.column1 = table1.column6)
where exists (select 1 from table2 t2 where t2.column1 = table1.column6);

MERGE is good for updating/inserting tables based on other tables. Something like this should work (would be a bit more clear with distinctly-named columns). It also accepts an optional 'WHEN NOT MATCHED THEN' clause which lets you insert new records.
MERGE INTO table2
USING (SELECT column2, column6 FROM table1) table1
ON (table2.column1 = table1.column6)
WHEN MATCHED THEN
update set column4 = column2;

Related

How to copy an sql column based on foreign key?

How do I run the following query
INSERT INTO table1
SELECT sourceId
FROM table2
WHERE table2.id = table1.productId
given that table2 has an id column and a sourceId column (and few others) and table1 already contains productId and I want to copy the sourceId column into the table as well?
The error message I'm getting with this query is simply "Unknown column 'table1.productId' in where clause", but if I include table1.productId in the SELECT and table1 on the FROM row, I get the "Column count doesn't match value count at row 1" error.
Any idea how to fix up the query?
when you are inserting into a table, you have to specify what to insert in all columns of that table. Should look like this:
INSERT INTO table1 (column1, column2, column3, ...)
SELECT value1, value2, value3, ...
FROM table2
WHERE ...;
But in your case since you said table1 already contains productId, you probably should do an UPDATE and not an INSERT
UPDATE table1 t1
SET t1.sourceId =
(SELECT sourceId
FROM table2
WHERE table2.id = t1.productId);
If this works for you, you can improve on it with JOINs
The select part is supposed to work stand-alone, so you must select from table1 in that part, if you want to refer to its rows.
INSERT INTO table1
SELECT sourceId
FROM table2
WHERE table2.id IN (SELECT productId FROM table1);

Can I UPDATE multiple records using different values in a SELECT query?

For example I have table1 with field1 and field2 and want to do something like:
UPDATE table1
SET field1, field2 = (SELECT field1, field 2 FROM tableXYZ)
WHERE field 3 = 'foobar'
or do I have to do multiple SETs, running the same SELECT query several times?
Assuming whatever database you are using supports it, you can join the tables. SO:
Update table1
set field1 = tbx.field1,
field2 = tbx.field2
from table1 join tablexyz on --some key value join
You can do a tuple assignment by putting the columns on the left hand side between parentheses.
UPDATE table1
SET (column1, column2) = (SELECT col1, col2
FROM tableXYZ
WHERE ...)
WHERE column3 = 'foobar';
The above is standard SQL, but not all DBMS support that.
Note that you have to use a WHERE clause in the sub-select to make sure that select only returns a single row (you would typically make that a co-related sub-query).

SQLite. "UPDATE table1 SET column1 = (SELECT column2 FROM table2);" is not working

Why this construction is not working in SQLite:
UPDATE table1 SET column1 = (SELECT column2 FROM table2);
where column1 and column2 has the same number of rows and the same type.
Use the common column to look up the matching record:
UPDATE table1
SET column1 = (SELECT column2
FROM table2
WHERE table2.Name = table1.Name)
It is not going to work because the table1.column1 only wants one value. What you are actually doing is setting table1.column1 = all the results from table2.column2.
You will probably have to join the two tables together on a common column or key
Because nested select returns more than one row probably. You try to put all the data from table2 into 1 element in table1
Is Column1 just getting the same value all the way down the column? Otherwise, you need to do a JOIN in the update here and Set Column1 = Column2 based on the join.
You are not linking the two tables
so it is trying to put all column2 values into each table1 row.
Link the two together with a field such as
Update table1 set column1 = column2
from table2 where table1.ColumnA = tables2.ColumnA
The reason why your statement isn't working is because you're telling it to put all of the values for column2 into every column1 cell in table1. You want something more like:
UPDATE
table1
SET
table1.column1 = table2.column2
FROM
table1
INNER JOIN
table2
ON
table1.id = table2.table1_id

SQL Replace used during a compare

I currently run this query:
UPDATE table1
SET column1 = table2.columnA
FROM table2
WHERE column2 = table2.columnA
AND column3 = table2.columnC
Yes there is a duplicate column, sorry.
When I cross reference column2 with table2.columnA there is a chance to get NULL because column2 DOES NOT contain / where as table2.columnA may contain /
I don't want to change the data in table2.columnA
It is my understanding that I can run this query to REPLACE the characters.
SELECT REPLACE ([table2.columnA],'/','-')
FROM table2
It is necessary that this does not make a permanent change to table2 so I want to make sure that I am getting this right or to see if there is a better way.
Now I want to combine the two queries but am unsure how.
UPDATE table1
SET column1 = table2.columnA
FROM table2
WHERE column2 = (SELECT REPLACE([table2.columnA],'/','-')table2.columnA)
FROM table2
AND column3 = table2.columnC
Thanks for the help!
Something like
UPDATE table1
SET column1 = table2.columnA
FROM table1
inner join table2
on table1.column2 = REPLACE([table2.columnA],'/','-')
and table1.column3 = table2.columnC

Insert a Row with values from multiple tables (SQL Server)

I'm defining a delete trigger, and I need to backup the row deleted but only a few arguments from the original and including one column from other table:
TableC:
* Column 1: value from a column in TableA
* Column 2 to 6: values from colums 1,2,3,5,6 from TableB
All I want is something like this:
INSERT into TableC values (
(SELECT Column1A from TableA where TableA.Column = 'SomeValue'),
(SELECT column1, column2, column3, column5, column6 from TableB));
The result on TableC must be:
Column1A , column1, column2, column3, column5, column6
But that is not working.
In my special case, TableB is the deleted table accessible only in triggers.
I'm using SQL-Server 2008 but all I need is the logic of the query, and then I try to translate it.
Thank you.
In another case, which I honestly find very odd, that might be closer to what you're describing, this might work:
INSERT INTO MyTable
SELECT
(SELECT ColumnA FROM Table1),
Table2.ColumnA,
Table2.ColumnB,
Table2.ColumnC,
Table2.ColumnD
FROM
Table2
That way, you're only selecting that one column from Table1, yet selecting many columns from Table2, regardless of any relationship between them.
You need to do joins on the select.
Here's an example:
INSERT INTO MyTable
SELECT
Table1.ColumnA,
Table1.ColumnB,
Table2.ColumnA,
Table2.ColumnB
FROM
Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID
This is just an example of "inserting from two tables".
You need to modify it to match what you're looking for, which I did not comprehend from your arbitrary example.
You can use a cross join to join unrelated tables:
INSERT MyTable
(col5, col7)
SELECT t1.col5
, t2.col7
FROM Table1 t1
CROSS JOIN
Table2 t2
WHERE t1.ID = 'SomeValue'
and t2.ID = 'OtherValue'
If one of the tables just contains one row, you can omit the where part for it.