I have two tables with the same column anomaly_id. I want to copy the row of anomaly_id from the first table to the second table using this code
UPDATE amb.anamoly_log_update
SET anamoly_id = t2.anomaly_id
FROM amb.anamoly_log_update t1
INNER JOIN amb.anomaly_fee t2 ON t1.anamoly_id=t2.anomaly_id
Even after I did that it shows 0 rows affected, when there is data in amb.anomaly.fee (source table)
Please help
Edit: Comment from post: I just want to copy all the anamoly_id from amb.anamoly_fee to amb.anamoly_log_update. My code might be nonsensical. Please do review it.
To copy the id from anomaly_fee to anamoly_log_update use :
INSERT INTO anamoly_log_update (anamoly_id)
SELECT anamoly_id FROM anomaly_fee
with both columns it looks like that:
INSERT INTO anamoly_log_update (anamoly_id,PID)
SELECT anamoly_id,PID FROM anomaly_fee
You only would copy the data if they where in both tables .. and then there is nothing update because you do not change the data => 0 rows affected
ON t1.anamoly_id=t2.anomaly_id
please think about what you really want to do and change your description ..
Does amb.anamoly_log_update contain at least one row corresponding to the anamoly_id that's present in amb.anamoly_fee? You are trying to join on two tables on anamoly_id.
You need to provide other linkage between tables than t1.anamoly_id=t2.anomaly_id or the query will do nothing
merge into amb.anamoly_log_update as t1
using amb.anomaly_fee as t2
on t1.anamoly_id=t2.anomaly_id
when matched then
update set t1.anamoly_id = t2.anomaly_id
Related
Just started using SSMS and having difficulties getting simple SQL commands to work. I have tried using the Import/Export Wizard, but it won't work as the destination table have columns that aren't available in the source column, and those columns don't allow NULL values.
So, I need to write a SQL command where I can specify those column with values that aren't NULL. I believe the correct way to achieve this is to use UPDATE? I have been experimenting with just updating the Primary Key:
UPDATE db1.dbo.Customer
SET No_ = v4.No_
FROM db2.dbo.Customer as v4
When executing I get the following message:
(0 row(s) affected)
The basic idea for an update from two tables (regardless of database) in SQL Server would be something like this:
UPDATE c1
SET No_ = v4.No_
FROM db1.dbo.Customer c1 JOIN
db2.dbo.Customer v4
ON c1.CustomerId = v4.CustomerId;
In other words, you need to identify how the rows match between the two tables, and then you can set values in one table to values in the other.
UPDATE db1.dbo.Customer
SET No_ = c1.No_
FROM db2.dbo.Customer as 'c1'
WHERE
db1.dbo.Customer.CustomerID = c1.CustomerID
Can you help me on this one.I'm trying to pull data from the database of a CAD software and I wish to make a temporary table from the given table below(the output temptable is also shown below) so that i can join it to my already created table1. I'm new to SQL and it seems that a temporary table could work but i don't know how to append the data from the other row into the first row such that the behavior is similar to a sum() function but working with text. Since i cannot post pictures yet, bear with me the formatting of the original table. and the temptable i wish to make. Thanks in advance
orignal table
----Oid---- ----Cable Tray----
--0010f--- ---mv001---
--0010f--- ---mv002---
--0010f--- ---mv003---
--020ab--- ---lv001---
--020ab--- ---lv002---
output temptable
----Oid---- ----Cable Tray Route---
--0010f--- ---mv001, mv002, mv003---
--020ab--- ---lv001, lv002---
This is my sample code:
select *
from table1
join temptable on temptable.oid=table1.oid
this query is working fine.
UPDATE data
SET unit_id='a3110a89'
WHERE unit_id='7d18289f';
Now, I need to run this query over 30 times
so I made csv file import it to the DB with this command:
COPY mytable FROM 'D:/test.csv' WITH CSV HEADER DELIMITER AS ','
Now I have table called my table with 2 columns OLD and NEW
i want to search the table "data" in column unit_id anywhere there if the value equals to the value in table "mytable.old" replace it with the value "mytable.new" on the same row.
I tried to run this query but I get an error:
UPDATE data
SET unit_id=(SELECT mytable."old" FROM public.mytable)
WHERE unit_id=(SELECT mytable."new" FROM public.mytable)
error:
more than one row returned by a subquery used as an expression
I think i'm just trying to do it in the wrong way...
thx for the help!
by the way Im using PostgreSQL
Your subqueries need to be correlated to the outer update:
UPDATE data
SET unit_id = (SELECT mytable."new" FROM public.mytable where data.old = mytable.old)
WHERE unit_id in (SELECT mytable."old" FROM public.mytable);
That is, set the unit_id to the "new" value, when you find the "old" value in the table.
Can you try like this,
UPDATE data A
SET A.unit_id=B.old
FROM (SELECT mytable."old",mytable."new" FROM public.mytable) B
WHERE A.unit_id=B.new
UPDATE data A
SET unit_id = B."old"
FROM public.mytable B
WHERE A.unit_id = B."new"
;
BTW: it looks like you also have old and new swapped in your question. Do you really want A's value to be set to B's old field?
I have a table that looks like the below table:
Every time the user loan a book a new record is inserted.
The data in this table is derived or taken from another table which has no dates.
I need to update this tables based on the records in the other table: Meaning I only need to update this table based on what changes.
Example: Lets say the user return the book Starship Troopers and the book return is indicated to Yes.
How do I update just that column?
What I have tried:
I tried using the MERGE Statement but it works only with unique rows of data, meaning you get an error if the same ID appears more than once.
I also tried using a basic UPDATE Statement and a JOIN but that's not going well.
I am asking because I have ran out of ideas.
Thanks for reading
If you need to update BooksReturn in target table based on the same column in source table
UPDATE t
SET t.booksreturn = s.booksreturn
FROM target t JOIN source s
ON t.userid = s.userid
AND t.booksloaned = s.booksloaned
Here is SQLFiddle demo
You can do this by simple Update & Insert statement.....
Two table A & B
From B you want to insert data into A if not exists other wise Update that data....
,First Insert into temp table....
SELECT *
INTO #MYTEMP
FROM B
WHERE BOOKSLOANED NOT IN (SELECT BOOKSLOANED
FROM A)
,Second Check data and insert into A.
INSERT INTO A
SELECT *
FROM #MYTEMP
And at last write one simple update statement which update all data of A. If any change then it also reflect to that data otherwise data as it is.
You can also update from #MYTEMP table.
If only one row with a distinct field is to be updated,I can use:
insert into tab(..) value(..) on duplicate key update ...
But now it's not the case,I need to update 4 rows inside the same table,which have its field "accountId" equal to $_SESSION['accountId'].
What I can get out of my mind at the moment is:
delete from tab where accountId = $_SESSION['accountId'],
then insert the new rows.
Which obviously is not the best solution.
Has someone a better idea about this?
Use the update just like that!
update tab set col1 = 'value' where accountId = $_SESSION['accountId']
Moreover, MySQL allows you to do an update with a join, if that makes your life a bit easier:
update
tab t
inner join accounts a on
t.accountid = a.accountid
set
t.col1 = 'value'
where
a.accountname = 'Tom'
Based on your question, it seems like you should review the Update Statement.
Insert is used to put new rows in - not update them. Delete is used to remove. And Update is used to modify existing rows. Using "Insert On Duplicate Key Update" is a hackish way to modify rows, and is poor form to use when you know the row is already there.
load all of the values in to a temporary table.
UPDATE all of the values using a JOIN.
INSERT all of the values from the temp table that don't exist in the target table.
You can use replace statement. This will work as a DELETE followed by INSERT