Updating table with another table - sql

I need to select entire columns from table compression_query_table into Tenth_mile but INSERT deletes columns already in that Table that aren't in Compression_query_Table and I cannot figure out how to use UPDATE on multiple columns at the same time.

Considering your session_name is unique, you could probably write two separate statements: INSERT and UPDATE for this:
Update Statement:
UPDATE Tenth_mile
INNER JOIN compression_query_table cq on Tenth_mile.session_name = cq.session_name
SET Tenth_mile.State = cq.State,
Tenth_mile.route_number = cq.route_number;
Insert Statement:
INSERT INTO Tenth_mile (session_name, state, route_number)
SELECT session_name
,STATE
,route_number
FROM compression_query_table cq
WHERE NOT EXISTS
(SELECT tm.session_name
FROM Tenth_Mile tm
WHERE tm.session_name = cq.session_name
);
Note: I don't have MS-Access installed on my machine, so I don't have
a way to test this. Also, I would run the update before running the insert statement (aka upsert).
Hope this helps!

Related

Updating a lot of Data in One Field using Where clause

I am trying to update one filled with a lot of data.
This is my table
I want to update NameAlias with new names for Major but this is my question if I have a lot of major names and ids how can I write a query that is good and short?
I know it should be something like this for one column.
Update Major Set NameAlias='Mathemathic' where IdMajor=1;
But what should I do if I have a lot of data for one field?
I want my result to be something like this:
You can use case when expression -
Update Major Set NameAlias=
case when namemajor='Math' then 'Mathemathic'
when namemajor='Computer' then 'Software'
when namemajor='Art' then 'Painthing'
when namemajor='History' then 'FranceHistory'
when namemajor='Music' then 'Piano' end
You should create a temp table containing the id, NameAlias accordingly like below.
select Id, NameAlias
into #TempTable
from (
values(1, 'Mathemathic'),
(2, 'Software'),
(3, 'Painthing')
-- The rest of values
)v(Id, NameAlias)
Then you can update the Major table
Update m
SET m.NameAlias = t.NameAlias
From Major m
INNER JOIN #TempTable t on m.IdMajor = t.Id
There is no need to create a temporary table to do this. Just use a derived table in the update query:
update m
set m.NameAlias = v.newAlias
from Major m join
(values(1, 'Mathematic'),
(2, 'Software'),
(3, 'Painting')
) v(idMajor, newAlias)
on m.IdMajor = v.IdMajor;

Insert follow by delete does not work in SQL Server

In SQL Server when I execute these two commands together
INSERT INTO TABLEB SELECT * FROM TABLEC WHERE TABLEC.COLUMNC = 'ABC'
DELETE TABLEC where TABLEC.columnC = 'ABC'
I only get the delete result. Insert did return a message saying x row affected but the content in the table remains empty.
Actual code
INSERT INTO STORECODE_BK SELECT * FROM STORE WHERE STOREID = '334'
DELETE STORE where STOREID = '334'
geomagas Had gotten the solution
Is due to the ON DELETE CASCADE attribute which delete the relevant PK
Thanks
Try the following.
INSERT INTO STORECODE_BK SELECT * FROM STORE WHERE STOREID = '334'
GO
DELETE FROM STORE where STOREID = '334'
GO
Select into a temporary table.
Then use that to insert into TableB and maybe delete from Table C
Then select from the temporary table.
Got to say all the hoops you are having to jump through indicates a less than great design.

Oracle SQL update

I've tried searching for this particular topic here, but haven't found the answer... Anyway, my aim is to update table (let's call it t_item), specifically column owner_id with values depending on another table (t_item_geo which is in turn linked to t_geo).
I'm not entirely sure whether the syntax below is actually valid for update statements.
UPDATE t_item SET owner_id= 6993 WHERE t_item.owner_id in
(SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
);
Anyway, my problem with this query is that it updates far more rows than it should - if I separate just the select statement Oracle returns ~750 rows but the udpate itself updates more than 4000 rows. It's almost as if the condition was completely ignored - which would point me to perhaps incorrect syntax.
I need to update specific value in the table based on the select from few other 'joined' tables. Hope it makes sense.
Thanks for any contribution!
UPDATE: sorry - maybe it wasn't clear from the question itself, but the correct number of edited items should be ~750 and not ~4000. Thanks!
try this
MERGE INTO t_item
USING
(
SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo,
t_item.rowid rowid_sub
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
) on (rowid = rowid_sub)
WHEN MATCHED THEN
UPDATE SET owner_id= 6993;

SQL Insert/Update Issue

I am trying to update one table from another, im able to update fine as long as the customer record exists, but there are some entries that dont.
To solve this i've tried running the following insert
SELECT *
INTO SalBudgetCust
FROM SalBudgetCust_temp
WHERE NOT EXISTS (
SELECT Customer
FROM SalBudgetCust
WHERE Customer = SalBudgetCust_temp.Customer
)
but im prompted with
There is already an object named 'SalBudgetCust' in the database.
Im stuck at this point... could anyone offer a little guideance?
SELECT INTO implicitly creates the table you name. You should instead use INSERT INTO ... SELECT * FROM ..., so that the existing table is used.
It should be INSERT INTO instead of SELECT * INTO ... like
INSERT INTO SalBudgetCust SELECT * FROM SalBudgetCust_temp
WHERE NOT EXISTS
(
SELECT Customer FROM SalBudgetCust WHERE Customer = SalBudgetCust_temp.Customer
)
The general syntax to insert data of one table into another is :
INSERT INTO new_table
SELECT * FROM old_table
WHERE some_condition;
Where, new_table is the table where you want to insert data, old_table is table from where you are fetching data and some_condition is the expression / condition based upon which you want to fetch data from old table.
You may use other clauses like order by, group by, and even sub queries after where clause.
May refer this SQL INSERT INTO and it's subsequent pages.

Update multiple columns in a TABLE from another TABLE (Oracle)

I would like to update multiple columns in one table based on values in another.
I think I know how to write an update statement in T-SQL that does what I want (haven't tested the below). Problem is I'm trying to translate this for an Oracle database. Does anyone know how to do the following in Oracle:
UPDATE oldauth SET
AUTHUNIQUENAME=newauth.AUTHUNIQUENAME
DESCRIPTION=newauth.DESCRIPTION
MAPPINGAUTHNAME=newauth.MAPPINGAUTHNAME
FROM
(SELECT * FROM USERS1 WHERE AUTHSOURCEID=100) oldauth
LEFT JOIN
(SELECT * FROM USERS2 WHERE AUTHSOURCEID=200) newauth
ON
oldauth.AUTHUSERNAME=newauth.AUTHUSERNAME;
MERGE
INTO (
SELECT *
FROM users1
WHERE AUTHSOURCEID=100
) oldauth
USING (
SELECT *
FROM users2
WHERE AUTHSOURCEID=200
) newauth
ON oldauth.AUTHUSERNAME=newauth.AUTHUSERNAME
WHEN MATCHED THEN
UPDATE
SET AUTHUNIQUENAME=newauth.AUTHUNIQUENAME,
DESCRIPTION=newauth.DESCRIPTION,
MAPPINGAUTHNAME=newauth.MAPPINGAUTHNAME