update query for multiple rows with different where condition? - sql

How to write update query in sql to update multiple rows with different where condition ?For example if i want to change the name of 100 rows of a particular table with different id's?
update table set name = 'value1' where id=1,
set name ='value2' where id=2;
But like this i can't write for 100 entries. . Any help?

You can create a temp table with these 100 entries then update the table by joining this new table, something like:
CREATE TABLE Temp(
Id int NOT NULL,
Name Varchar(50)
) ;
UPDATE YourTable t1
INNER JOIN Temp t2 ON t1.Id = t2.Id
SET t1.Name = t2.Name;

Related

SQL Update using data from other tables and foreign keys

I am trying to update a column from table1 based off of data from two other tables.
Table 1 has columns id, columnIWantToUpdate, table2FK, table3FK
Table 2 has columns table2FK, table2_unique_id
Table 3 has columns table3FK, table3_unique_id
So I get table2_unique_id and table3_unique_id as inputs and I want to use the columns table2FK and table3FK to update table 1 based off of the unique_ids I received as input
One method uses filtering in the where clause:
update table1
set columnIWantToUpdate = ?
where exists (select 1
from table2 t2
where t2.table2FK = table1.table2FK
) or
exists (select 1
from table3 t3
where t3.table3FK = table1.table3FK
);
It is not clear if you want and and or for the conditions.

How do I insert values into a new table using SQL WHERE clause for matching?

I have two tables: Table 1 is the master table and Table 2 is a table that is refreshed monthly and truncated after. Thus, I want to write a query that inserts monthly new values from Table 2 into Table 1 using some matching fields. The columns I need to match by are country and name. Here are the tables:
How do I approach this query to get to the green table? I looked into Update and Insert Statements but could not find anything which could help.
How about using MERGE statement
MERGE INTO Table1 as Target
USING Table2 as Source
ON (target.Name = source.Name AND target.Country = source.Country)
WHEN MATCHED THEN UPDATE SET Age = source.Age, Gender = source.Gender
WHEN NOT MATCHED THEN INSERT (Name, Country, Age, Gender) VALUES (source.Name, source.Country, source.Age, source.Gender);
What the statment does is look for records in Target that match records in source by name and country. If a Match is found, the Age and Gender of the target records are update, when a match is not found, a new record is inserted in target table.
I tested this in SQL Server, but I think snowflake has the same syntax.
You can read about MERGE statement in Snowflake documentation.
Snowflake syntax
UPDATE
table1 AS TA
SET
TA.Age = TB.Age
FROM (SELECT TA1.country, TB1.country, TA1.name, TB1.name, TB1.Age
FROM table1 AS TA1
INNER JOIN table2 AS TB1
ON (TA1.country = TB1.country AND TA1.name= TB1.name)) TB
WHERE TA.country = TB.country
And MySql
UPDATE
table1 AS TA
INNER JOIN table2 AS TB
ON TA.country = TB.country
AND TA.name= TB.name
SET
TA.Age = TB.Age;
Considering you have to only update the age column in the table1.
update table1 set AGE=(select distinct age from table2 t2 where t2.COUNTRY=table1.COUNTRY and t2.NAME=table1.NAME);
or
UPDATE table1 SET table1.age = table2.age FROM table1 , table2 where table1.country = table2.country and table1.name = table2.name
Snowflake syntax is a bit different here. Something like this should work:
UPDATE table1 TA
SET TA.Age = TB.Age
FROM table2 TB
WHERE TA.country = TB.country
AND TA.name = TB.name;
As a note, the documentation on Snowflake is excellent. The syntax for this is located here: https://docs.snowflake.com/en/sql-reference/sql/update.html

ORACLE - Update a value from two different columns in differente tables with a filter

I'have a little question about a query.
I have to update a column from a table where there are only record of expense(integer).
I must increase the expense of 5% if the client is from a specific state, the column of the state is in a different table and the key in common is the address.
This is my query below :
UPDATE table 1 a
SET expense_vl = (
SELECT expense*1.05 FROM table 1
LEFT JOIN table2 b ON b.ADDRESS_ID=a.ADDRESS_ID
WHERE description_state IN 'lollyland'
)
I'd recommend using a semi-join:
update table_1 a
set expense_v1 = expense * 1.05
where exists (
select null
from table2 b
where
a.address_id = b.address_id and
b.description_state = 'lollyland'
)
Althought I must add that it would help if you include the DDL for your table. We're sort of guessing at which table "description" came from.
Also, when possible, include sample input for each table and desired output. We don't need a million records, just an example that illustrates your issue.
Try below
UPDATE table1 a SET expense_vl = (SELECT expense*1.05
FROM table2 b
WHERE b.ADDRESS_ID=a.ADDRESS_ID)
WHERE description_state IN 'lollyland'
Or try with subselect:
UPDATE table1
SET expense_vl = expense*1.05
WHERE ADDRESS_ID IN (SELECT ADDRESS_ID FROM table2 WHERE description_state IN 'lollyland')
I think you need to change your query like below :
UPDATE table 1 A
SET expense_vl=expense*1.05 FROM table 1
LEFT JOIN table2 B ON B.ADDRESS_ID=A.ADDRESS_ID
WHERE B.description_state IN 'lollyland'

Selecting rowset when value exists in one of 5 tables with different amounts of columns

Using SQL Server, I Need to return the entire row from whatever table contains 'value' in the Filename column (A column each of the tables contain), but the tables do not have the same number of columns, and each table has unique columns with their own specific data types (The only column Name/Type they have in common is the Filename column that I need to check for 'value').
Ideally, I would be able to do something along the lines of:
SELECT * FROM Table1, Table2, Table3, Table4, Table5
WHERE Filename = 'someValue'
Since all tables share the same column name for the Filename.
I have tried using Union but have issues since the number of columns and datatypes of the tables do not align.
I have also tried every combination of JOIN I could find.
I'm sure this could be accomplished with IF EXISTS, but that would be many, many lines of what seems like unnecessary code. Hoping there is a more elegant solution.
Thanks in advance!
You can try to join the tables together. First create temporary table where you store the input. And then join the tables with this temporary to get all records you want. When there is no record for that filename in the table, then you will get NULL values.
create table Table1 (id int,value int);
insert into Table1 values (1,10)
create table Table2 (id int,value int);
insert into Table2 values (1,20)
create table Table3 (id int,value int);
insert into Table3 values (2,30)
Here is the query itself
create table #tmp (id int)
insert into #tmp
values (1)
select t.id, t1.value, t2.value, t3.value from #tmp as t
left join Table1 as t1
on t.id = t1.id
left join Table2 as t2
on t.id = t2.id
left join Table3 as t3
on t.id = t3.id
And this is what you get
id value value value
1 10 20 NULL
this should work too:
EXEC sp_MSforeachtable
#command1='SELECT * FROM ? where filename = ''someValue''',
#whereand='AND o.id in (select object_id from sys.tables where name in (''Table1'',''Table2'',''Table3''))'

Oracle update using subquery

I need to convert my update Ms SQL Server query for Oracle
I have two tables:
Table1(id int, SaveId int, ChangeId int)
Table2(id int, smSaved int, boldId int)
The query is:
UPDATE
Table1
Set
SaveId=tbl.smSaved
FROM
Table1 tbl1
join Table2 tbl on tbl1.ChangeId=tbl.boldId
In MsSql everything works fine, but when I'm trying to run it in Oracle thats doesn't works. I can't figure out whats wrong with it.
Can anybody explain me what I need to change?
Try this:
UPDATE table1 SET SaveId =
(SELECT tbl.saved FROM table2 tbl
WHERE tbl.boldId = table1.ChangeId)
WHERE EXISTS(
SELECT tbl.saved FROM table2 tbl
WHERE tbl.boldId = table1.ChangeId
)
The first part of query update SaveId with value of tbl.saved (I suppose you have only one row of table2 linked with table1.
The second part (exists) guarantee you have a correspond between two table (if you add exists you put NULL where not exists row in table2)
You are may to use update with subquery same MS SQL. In Oracle its look like:
UPDATE (SELECT t1.savedId t1_saved
, t2.smsaved t2_saved
FROM table1 t1
, table2 t2
WHERE t1.changeid = t2.boldid)
SET t1_saved = t2_saved