0 rows updated while using update query in oracle - sql

When I am trying to update a column with character datatype using update statement. It says 0 rows update and does not throw any error. As the number of columns are much i tried using where clause using specific column out of it...where it updates the row.
Issue is when all the columns are used to alter the value, it gives back 0 rows updated. I have to automate it in vba code/tool so that it could update using all the rows. I identified the columns using which the value is not being updated but there is no error return.
It only says:
0 rows updated.
But what is wrong with that specific column(datatype...decimal).
Please help. I have searched whole internet but no results.

If you are trying to update a specific value in your database and only that one, you may have a rounding issue. This mean the value you see is not the exact value stored.
So you may try to update it with a specific range.
First select what you want to update:
select rowid from table_1
where val_x between <x>-<upsilon> and <x>+<upsilon>;
Update if you have only one value;
update table_1 set val_x = <new_val>
where val_x between <x>-0.001 and <x>+0.001
;
(otherwise, if more than 1 value, decrease <upsilon> to 0.0001, etc.)
Hope it helps

Related

Adding or removing columns in result set of sql query depending on value of other field

Assume I have a query that returns a result set of columns A and B from table First_Table. I want to limit the result set to those columns if the value of column X in table Second_Table is 0, and I want to add column C from table First_Table if the value of column X is 1.
The problem is easily resolved using a Python for example whereby I just have a variable as an empty string if value in column X is 0 or it would be equal to the string 'First_Table.ColumnC AS [Dynamic Value],', and I just format the sql in the script accordingly.
If Else solution is not an elegant way because I have multiple columns to add dynamically depending on multiple values...
I am just looking for some ideas on directions.. I have been looking at this for a while, might be bogged up
Dynamic sql is the best way to resolve this as suggested in the comments.

How to update when subquery returns multiple rows (with cursor)

update tac_master
set msg = (select 'Yes,this is a 4G supportive handset.Marketing Name'||MKT_NAME||' of LTE:1800 Manufacturer#'||manufacturer
from tac_master)
where lte_1800='Yes';
Attached is table data and structureTable sample
As for the code you specified, just change the subquery to value as you are updating it with constant value - you dont't need to query it again to get the mkt_name and manufacturer values as they're available in update statement:
update tac_master
set msg = 'Yes,this is a 4G supportive handset.Marketing Name'||MKT_NAME||' of LTE:1800 Manufacturer#'||manufacturer
where lte_1800='Yes';
as for general question with updating a row while subquery is returning multiple values - you can't, that's why you get this error, Oracle has to know with which value the current has to be modified and by doing it your way you're not defining it

insert made instead of update resulting in duplicate rows. how to fix?

I have a database where is supposed to have only one register with 2 value columns that can be filled by my web application. The values to one of the columns where given to me in an excel and we had to put it into the database. The person who did that, should had used an "update if exists else insert" but he didn't. Now, we have for some data, duplicate lines, one having just the column "valor_realizado_oficial" filled (with the column adt_login filled with 'talend' and with the key columns filled too), and another with the other column filled by the application.
So:
If exists two lines, I would like to copy the value of the column "valor_realizado_oficial" from the line with adt_login like 'talend' to the other line and delete this line.
If exists just one line, do nothing.
I tried to perform the copy part with:
update indicador_val iv
set valor_realizado_oficial=carga.valor_realizado_oficial
from (
select valor_realizado_oficial, ano, municipio_fk, indicador_fk, und_federativa_fk
from indicador_val
where adt_login like 'talend' and ano=2013 ) carga
where iv.ano=2013 and iv.municipio_fk=carga.municipio_fk
and iv.indicador_fk=carga.indicador_fk and iv.ano=carga.ano
and iv.und_federativa_fk = carga.und_federativa_fk;
But 0 rows where affected. Here's an example of a pair of lines:
id; adt_login; ano; valor_estimado, valor_realizado_oficial, indicador_fk, municipio_fk, und_federativa_fk
313885; "talend";2013;;888;2;2202;
291998;"suagenda";2013;900;;2;2202;
And I would like to have just the second, with values:
291998;"suagenda";2013;900;888;2;2202;
What I did wrong? Thanks.
In the sample data, there are empty columns. For example, the last one: und_federativa_fk.
Some of these columns are used in your joining conditions in the correlated UPDATE. If these empty values translate to the SQL NULL value, you should realize that NULL=NULL as a condition is going to be NULL, which means false in this context.
That alone might explain why no row get updated.
The solution is to use IS NOT DISTINCT FROM as the comparison operator for values that may be null.
Example:
where iv.ano=2013 and iv.municipio_fk IS NOT DISTINCT FROM carga.municipio_fk
and iv.indicador_fk IS NOT DISTINCT FROM carga.indicador_fk
etc...

Check if unique value exists before updating in MSSQL

I have a table containing a list of MAC addresses that in some cases has been padded to twelve digits and in other cases has not.
ie: 00FF12345678 OR FF12345678
There are cases where both values exist in this table. I have a function that pads the value with the two zeroes and I can count the occurrence of these duplicates, but I don't have the time or patience to manually remove the duplicates or pad those that do not already have a record.
I figured I could update the 10 digit values to their correct value with the following statement:
UPDATE [rs_DEVICES]
SET [device] = dbo.pad_zero(12,[device])
WHERE LEN([device]) < 12
Of course this doesn't work because I need to only update those values that do not have an existing 12 digit record. The unique constraint on this column stops me from doing this.
I need some sort of IF EXISTS statement in the WHERE clause but I haven't been able to come up with anything that works.
Using MSSQL Server 2008 R2.
Thoughts?
This does the job for you:
UPDATE [rs_DEVICES]
SET [device] = dbo.pad_zero(12,[device])
WHERE LEN([device]) < 12 AND NOT EXISTS (
SELECT 1 FROM rs_DEVICES r1 WHERE r1.device = dbo.pad_zero(12,[device])
)
I think it's self explanatory, but if you have any questions please comment.
Honestly though, I recommend you to fix the real problem. Reassign the current references to the length=10 records to their respective length=12 siblings and then remove them.

Update A multi-valued field in Access

I have created a lookup table in Access to provide the possible values for a column. Now I need to update this column with the data it had before I converted the column. I am unable to figure out a SQL Query that will work. I keep getting the error "An UPDATE or DELETE query cannot contain a multi-valued field." My research has suggested that I just need to set the value of the column but this always updates 0 records:
UPDATE [table_name] SET [column_name].Value = 55 WHERE [table_name].ID = 16;
I know this query will work if I change it to update a text column, so it is definitely a problem with just this column.
If you're adding a value to your multi-valued field, use an append query.
INSERT INTO table_name( [column_name].Value )
VALUES (55)
WHERE ID = 16;
If you want to change one particular value which exists in your multi-valued field, use an UPDATE statement. For example, to change the 55 to 56 ...
UPDATE [table_name]
SET [column_name].Value = 56
WHERE [column_name].Value = 55 And ID = 16;
See Using multivalued fields in queries for more information.
I have figured this out! It certainly was counter-intuitive! You have to use an INSERT statement to do the update.
-- Update a record with a multi-valued field that has no value
INSERT INTO [table_name] ( [[column_name].[Value] )
VALUES(55)
WHERE [table_name].ID = 16;
This confused me because I was expecting an UPDATE statement. I think it actually inserts a record into a hidden table that is used to associate multiple values with this column.
I am working with Sharepoint, I created the tables as multi-value fields, ran into the error with my INSERT INTO statement, went back to Sharepoint to change to non-multi-value fields, but that didn't fix it.
Recreated the table without using multi-value fields, and the INSERT INTO worked just fine.
do not use the .value part
UPDATE [table_name] SET [column_name] = 55 WHERE [table_name].ID = 16;
INSERT INTO Quals (cTypes.[value])
SELECT Quals_ContractTypes.ContractType
FROM Quals_ContractTypes
WHERE (Quals.ID = Quals_ContractTypes.ID_Quals);
I gotta say I didn't understand very well your problem but I saw something strange in your query. Try this:
UPDATE [table_name] SET [column_name]= 55 WHERE [table_name].ID = 16;
UPDATE:
Look at this link: it has an example
UPDATE Issues
SET Issues.AssignedTo.Value = 10
WHERE (((Issues.AssignedTo.Value)=6)
AND ((Issues.ID)=8));
NOTES
You should always include a WHERE
clause that identifies only the
records that you want to update.
Otherwise, you will update records
that you did not intend to change. An
Update query that does not contain a
WHERE clause changes every row in the
table. You can specify one value to
change.
The Multi-Valued field refers to Access databases that have tables with columns, that allow you to select multiple values, like a Combo Checkbox list.
THOSE are the only Access types that SQL cannot work with. I've tested all Access lookup possibilities, including hard-coded values, and lookup tables. They work fine, but if you have a column that has the Allow Multiple select options, you're out of luck. Even using the INSERT INTO as mentioned below, will not work as you'll get a similar but different error, about INSERTing into multi-valued fields.
As mentioned it's best to avoid using such tables outside of Access, and refer to a table specifically for your external needs. Then write a macro/vba script to update the real tables with the data from the "auxiliary" table.