I want to update a column 'XYZ_RCVD' to 'Y' if there is a value in the row, if not i want to update column 'XYZ_RCVD' to 'N'. I started with this simple script that worked but I had to modify it for the 'N' case. How would i merge both of them into one? I tried some IF-THEN-else but that didn't work too well with the INSERT statement.
Here is what i have so far.
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'Y'
WHERE XYZ_NAME IS NOT NULL;
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'N'
WHERE XYZ_NAME IS NULL;
The solution below by Habib worked perfectly.
I believe you are looking for CASE WHEN like:
UPDATE TEST_SURVEY
SET XYZ_RCVD = CASE WHEN XYZ_NAME IS NOT NULL THEN 'Y' ELSE 'N' END;
Related
I am running SQL Server and am needing to update hundreds of entries. Column A has unique values and based on that I need to update column B of the same table, all with different values. Essentially I am looking for a way to perform the following but in a bulk manner:
UPDATE table
set column B = 'value'
where column A = 'unique value'
Is this what you are expecting,
UPDATE table
set column B = case when column A = 5 then 'unique string' end;
How about:
update table set
columnB = concat('unique ', columnA)
you may, or may not, need a where clause, depending on your needs.
With hundreds of values you better have the {unique value - update value} mapping defined in a separate table as TT is asking. But otherwise, if you are okay with hard-coding those mappings in your UPDATE statement:
UPDATE t
SET B = CASE A
WHEN 'unique value1' THEN 'value1'
WHEN 'unique value2' THEN 'value2'
WHEN 'unique value3' THEN 'value3'
ELSE 'Unknown'
END
FROM table t
BTW, the above is only for 3 mappings
Here, you need to write cursor to update a column value in the same table
BEGIN
DECLARE c_a, c_b TEXT;
DECLARE c_ab CURSOR FOR
SELECT column_a,column_b FROM c_table;
OPEN c_ab;
LOOP
FETCH c_ab into c_ca, c_cb;
IF c_ca = "0" THEN
update c_table SET column_b = "No" WHERE
column_a = c_ca;
END IF;
IF c_ca = "1" THEN
update c_table SET column_b = "Yes" WHERE
column_a = c_ca;
END IF;
END LOOP;
CLOSE c_ab;
END
Working and tested code [please refer some cursor tutorials to update according to your condition] and with this you can update table in bulk and speedy
Thanks #Jim Macaulay. That did it. Thank you everyone else for your input.
I need to update newly created column in my oracle table. To do so I need to use existing values in row to decide how to populate this column, I am getting error:
java.lang.NullPointerException -> See Debug Output for details
This is my query:
UPDATE
SCHEMA_NAME.TABLE_NAME
SET
OCO= IF CO= 'Y' AND COM='Y' THEN
{
'Y'
} ELSE
{
'N'
}
END IF;
Any suggestions on syntax?
You could use CASE expression in the SET clause.
For example,
UPDATE table
SET schema.column = CASE
WHEN CO= 'Y' AND COM='Y' THEN
'Y'
ELSE
'N'
END
I am using PL/sql on APEX.
I have a simple table, where one of the column I need to update the field using Case or if condition.
Just can't this right.
e.g.
Update TableAA
set column1 =
Select case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end as column1
from TableAA;
Since you are updating records from same table, why not try:
Update TableAA set column1 =
case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end;
I am running an SQL query and have just two parenthesis but I still get the error "missing parenthesis". The code is shown below:
Update ALEKWE_CUSTOMER C
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE C.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
If this is for sql-server, then this might be the answer:
Update ALEKWE_CUSTOMER
set recently_purchased = (CASE WHEN EXISTS(
SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D WHERE customer_id=D.customer_id) THEN 'Y'
ELSE 'N' END);
You need to put the whole CASE structure in parentheses too.
Next to that, you can't use an alias for the table which will be updated.
You cannot have the alias C at this position, and neither do you need it:
Update ALEKWE_CUSTOMER
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE ALEKWE_CUSTOMER.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
You don't need to put the case into paranteses either.
You would need:
Update ALEKWE_CUSTOMER
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE C.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
FROM ALEKWE_CUSTOMER C
for your alias to work
Edit:
My altered statements work fine on Microsoft SQL-Server.
Apparently the problem is specific to Oracle SQL.
After reading your comments, I added Oracle to your tags, since you say this nowhere.
If there's:
IF UPDATE (col1)
...in the SQL server trigger on a table, does it return true only if col1 has been changed or been updated?
I have a regular update query like
UPDATE table-name
SET col1 = 'x',
col2 = 'y'
WHERE id = 999
Now what my concern is if the "col1" was 'x' previously then again we updated it to 'x'
would IF UPDATE ("col1") trigger return True or not?
I am facing this problem as my save query is generic for all columns, but when I add this condition it returns True even if it's not changed...So I am concerned what to do in this case if I want to add condition like that?
It returns true if a column was updated. An update means that the query has SET the value of the column. Whether the previous value was the same as the new value is largely irelevant.
UPDATE table SET col = col
it's an update.
UPDATE table SET col = 99
when the col already had value 99 also it's an update.
Within the trigger, you have access to two internal tables that may help. The 'inserted' table includes the new version of each affected row, The 'deleted' table includes the original version of each row. You can compare the values in these tables to see if your field value was actually changed.
Here's a quick way to scan the rows to see if ANY column changed before deciding to run the contents of a trigger. This can be useful for example when you want to write a history record, but you don't want to do it if nothing really changed.
We use this all the time in ETL importing processes where we may re-import data but if nothing really changed in the source file we don't want to create a new history record.
CREATE TRIGGER [dbo].[TR_my_table_create_history]
ON [dbo].[my_table] FOR UPDATE AS
BEGIN
--
-- Insert the old data row if any column data changed
--
INSERT INTO [my_table_history]
SELECT d.*
FROM deleted d
INNER JOIN inserted i ON i.[id] = d.[id]
--
-- Use INTERSECT to see if anything REALLY changed
--
WHERE NOT EXISTS( SELECT i.* INTERSECT SELECT d.* )
END
Note that this particular trigger assumes that your source table (the one triggering the trigger) and the history table have identical column layouts.
What you do is check for different values in the inserted and deleted tables rather than use updated() (Don't forget to account for nulls). Or you could stop doing unneeded updates.
Trigger:
CREATE TRIGGER boo ON status2 FOR UPDATE AS
IF UPDATE (id)
BEGIN
SELECT 'DETECT';
END;
Usage:
UPDATE status2 SET name = 'K' WHERE name= 'T' --no action
UPDATE status2 SET name = 'T' ,id= 8 WHERE name= 'K' --detect
To shortcut the "No actual update" case, you need also check at the beginning whether your query affected any rows at all:
set nocount on; -- this must be the first statement!
if not exists (select 1 from inserted) and not exists (select 1 from deleted)
return;
SET NOCOUNT ON;
declare #countTemp int
select #countTemp = Count (*) from (
select City,PostCode,Street,CountryId,Address1 from Deleted
union
select City,PostCode,Street,CountryId,Address1 from Inserted
) tempTable
IF ( #countTemp > 1 )
Begin
-- Your Code goes Here
End
-- if any of these "City,PostCode,Street,CountryId,Address1" got updated then trigger
-- will work in " IF ( #countTemp > 1 ) " Code)
This worked for me
DECLARE #LongDescDirty bit = 0
Declare #old varchar(4000) = (SELECT LongDescription from deleted)
Declare #new varchar(4000) = (SELECT LongDescription from inserted)
if (#old <> #new)
BEGIN
SET #LongDescDirty = 1
END
Update table
Set LongDescUpdated = #LongDescUpdated
.....