Effective update statement - sql

I have table Value_table and column value
I need to update value column as 'Hi' if any value column exists with 'bye'
I wrote query
UPDATE Value_table
SET value = 'Hi'
WHERE value_id
IN
(
SELECT
value_id
FROM
Value_table
WHERE
value = 'bye'
)
It works fine.Now since I'm using the same table in sub query can we make any other way to make this statement in more efficient.?

If value_id is unique you can do
UPDATE Value_table
SET value = 'Hi'
WHERE value = 'bye'

i guess if the table is really large, EXISTS might perform better.
UPDATE vt
SET vt.value = 'Hi'
FROM Value_table vt
WHERE EXISTS ( SELECT 1
FROM Value_table vt2
WHERE vt.value_id = vt2.value_id AND vt2.value = 'bye')
if any value column exists with 'bye' if that means the id does not have to match, you can remove the vt.value_id = vt2.value_id portion

Related

Moving a cell value to another row based on ID numbers

I am looking to move B to the above row. It can either be placed where the Null value is in Column B or another column can be created. The value of B is linked to value A through an ID. The ID for value B is always X + 2 (the values in the ID column are integers).
I can’t just move the value up because the table I am working with has thousands of rows. It must be linked to the ID’s.
Please let me know if you have any questions. Any assistance is much appreciated. Thank you.
ID
Column A
Column B
X
A
NULL
X+2
NULL
B
Keep in mind I am very new to SQL. Below is what I tried. It created a new column that only contains NULL values.
Select
Column_B
From
Table_Name
Where
Table_Name.ID = Table_Name.ID +2 ) AS Col_B_Value
You can use a conditional subselect for that
UPDATE Table_Name T1
SET Column_B = (Select
Column_B
From
Table_Name
Where
Table_Name.ID = T1.ID +2 )
WHERE Column_B IS NULL
Some databases could have a problem so you can make
UPDATE Table_Name T1
SET Column_B = (Select
T2.Column_B
From
(SELECT ID,Column_B FROM Table_Name) T2
Where
T2.ID = T1.ID +2 )
WHERE Column_B IS NULL
You could just do it with 2 updates statements
UPDATE Table
SET Column B = 'B'
WHERE ID = 'X'
UPDATE Table
SET Column B = NULL
WHERE ID = 'X+2'
If you need to do it in a select statement you could do it with a case statement too
SELECT ID,
Column A,
CASE WHEN ID = X AND Column B = NULL THEN 'B'
ELSE Column B END
FROM Table

How to update the Top (20) records with using Select Query

How to update the Top (20) records with using Select query?
Table1 (table records are in for loop) has 60 records. At a time, I want to get first 20 records based on table column="TEXT", then update those 20 records with column="TEXT1".
After that, I will pick the next 20 records (21-40) and again update as above.
I'm using below query, but it will update the first 20 records after next 20 (21-40) records it will not work.
Update tableName set Column = 'TEXT1' where column = 'TEXT' ;
or if need be conditionally update...
UPDATE tablename set column = case when column = 'TEXT' then 'Text1'
else column = 'OtherText' then 'Text1Other'
else column = 'StillOtherText' then 'Text1Other2' end
WHERE column in ('TEXT','OtherText','StillOtherText');
One way is to use a while-loop that checks for the existence of 'TEXT'. If the check returns true, then the top 20 primary keys are selected as part of an update statement.
WHILE EXISTS (SELECT *
FROM Table1
WHERE yourcolumn = 'TEXT')
BEGIN
UPDATE Table1
SET yourcolumn = 'TEXT1'
WHERE primarykey IN (
SELECT TOP 20 primarykey
FROM Table1
WHERE yourcolumn = 'TEXT'
)
END

Oracle SQL Queries - Change Field Value

How can I change field value with my query?
I would like to see “TR” when it is ‘TÜRKEI’ in the database.
SELECT * FROM LAND WHERE LNDNR_LAND = '111';
D XXX 01/01/0001 111 TÜRKEI
Try this
select col1,col2,...., case when colN ='TÜRKEI' then 'TR' else colN end as colN
from your_table
Assuming column name is col and you
you can use SQL Standard case:
select t.*, case when col = 'TÜRKEI' then 'TR' else col end as col_new
from your_table t;
or Oracle specific decode:
select t.*, decode(col, 'TÜRKEI', 'TR', col) as col_new
from your_table t;
If you want to update the table with the value, you can simple update
update your_table
set col = 'TR'
where col = 'TÜRKEI';
If you want to change the value in database then you have use Update statement.
The SQL UPDATE statement is used to update existing records in the tables.
e.g
update tablename
set column1 = value
where condition;

Compare each row values in second table in SQL Server?

I have a scenario where I have to search value of column 1 in first table to see whether it matches some value in another table.
This should continue in a loop until the last row on first table has been compared.
No loops needed. You can do this easily as a set based operation using exists()
select *
from FirstTable
where exists (
select 1
from SecondTable
where FirstTable.Column1 = SecondTable.Column1
);
To find the opposite, where the row in the first table does not have a match based on Column1, you can use not exists()
select *
from FirstTable
where not exists (
select 1
from SecondTable
where FirstTable.Column1 = SecondTable.Column1
);
If you want to identify which rows have a match and don't you can use:
select FirstTable.*
, MatchFound = case when x.Column1 is null then 'No' else 'Yes' end
, x.Column1
from FirstTable
outer apply (
select top 1
*
from SecondTable
where FirstTable.Column1 = SecondTable.Column1
) as x

In this example can UPDATE statements be combined?

I quite often have rows of code like the following:
UPDATE my_table SET name = 'x' WHERE Original = 'a'
UPDATE my_table SET name = 'y' WHERE Original = 'b'
UPDATE my_table SET name = 'z' WHERE Original = 'c'
UPDATE my_table SET name = 'k' WHERE Original = 'd'
UPDATE my_table SET name = 'm' WHERE Original = 'e'
UPDATE my_table SET name = 'n' WHERE Original = 'f'
Can I combine/shorten this code into one UpDate statement - or are they best just left as they are?
UPDATE my_table
SET name =
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
END
That will update EVERY row. So if there's an Original value you haven't specified, it will be set to NULL. So you might want to limit the update to just those you want to update, with a WHERE clause, like so:
WHERE Original IN ('a', 'b', ...)
OR, as an alternative, you could use an ELSE statement, which leaves the name value as is, if it doesn't have a match in the WHEN statements, like so:
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
ELSE name
END
You could use a case statement:
UPDATE my_table
SET name =
case Original
when 'a' then 'x'
when 'b' then 'y'
...
else name -- Preserve original
end
The else clause makes sure you're not modifying a name if it's not matched in the case.
You could use a table value constructor and a from clause, if these values aren't already in a table:
update mt set name = t.name
from
my_table mt
inner join
(values
('a','x'),
('b','y'),
('c','z'),
('d','k'),
('e','m'),
('f','n')
) t(original,name)
on
mt.Original = t.original