SQL Update or Replace function? - sql

I have a table which contains multiple columns.
Column 1 Column 2 Column 3
unique identifier alphanumerical value numerical value
The unique identifier is currently using the values from Column 2. If I wanted to use the values from Column 3 instead, which would be better suited for my situation? Replace or Update? Or is there another way I should go about doing this.
I'm using TOAD for Oracle for what it is worth.
Thank you.

Turns out what the OP wanted to do was simply set column1 to the value in column3, no replace was necessary. Just a straight update, as in:
UPDATE TheTable SET column1 = column3;

IF
you want to make a change in the table THEN
use UPDATE
ELSE IF
you want to just view the column1 values mapped with column3 values for particular instance THEN
use INSERT
The otherway around use UPDATE to change the contents of table, whereas replace is a function which really doesnt makes any changes in the contents of the table but just shows u the changed output.

You would use both.
update sometable set column1 = replace(column1,column2,column3)
You might want to do the following first to make sure you're replacing what you want to replace:
select replace(column1,column2,column3) from sometable

Related

Update query in Access to update MANY columns from Null to value

I have a database table with about 100 columns (bulky, I know). I have about half of these columns which I will need to update iteratively to set Is Null or "" values to "TBD".
I compiled all 50 some columns which need to be updated into an update query with Access SQL code that looked something like this...
UPDATE tablename
SET tablename.column1="TBD", tablename.column2="TBD", tablename.column3="TBD"....
WHERE tablename.column1 Is Null OR tablename.column1="" OR tablename.column2 Is Null OR tablename.column2="" OR tablename.column3 Is Null OR tablename.column3=""....
Two issues: This query with 50 columns receives a "query is too complex" error.
This query is also just functionally wrong...because I'm losing data within these columns due to the WHERE statement. Records that had values populated which I did not want to update are being updated because of the OR clause.
My question is how can I go about updating all of these columns and setting their null or empty values to a particular value (in this case, "TBD")?
I know that I can just use a select query to select the columns I need to update, run it, and just CTRL+H to find & replace "" to "TBD". However, I'm worried about the potential for this to introduce errors into my dataset. I also know I could also go through column by column and update these values via an update query. However, this would be quite time consuming with 50+ columns & the iterative updates which I need to run on the entire dataset.
I'm leaning towards this latter route. I am still wondering if there are any other scripted options which I can build into a query to overcome such an issue, and that leads me here to you.
Thank you!
You could just run 50 queries:
UPDATE table SET column1="TBD" WHERE column1 IS NULL OR column1 = "";
An optimization could be:
Create a temporary table which determines which rows actually would need an update: Concatenate all column values such that a single NULL or empty would result in an record in your temp table. This way you only have to scan the base table once.
Use the keys from that table to focus on those rows only.
Etc.
That is safe and only updates your empty values (where as your previous query would have updated all columns unless you would have checked every value first with an IFNULL).
This query style also does not run into the too complex issue
You could issue one query as:
UPDATE tablename
SET column1 = iif(column1 is null or column1 = "", "TBD", column1),
column2 = iif(column2 is null or column2 = "", "TBD", column2),
. . .;
If you don't mind potentially updating all rows, you can leave out the where clause.

Efficiently add a column with a value

I am trying to add a column to a tsql table, i do this using SMO in c#. Altering the table is fine but i want to set the column to have a value. The table contains 650 million rows and the update query is taking over a day and a half to run.
Update [TempDatabase].[dbo].[table1] set RawSource = 'DTP'
This is the query I am running above.
Can anyone think of a more efficient way of doing this?
Thanks in advance.
Sometimes, it is more efficient to copy the table with the new value and re-create the table in a single command. Also, you might want to be sure that you have minimal logging for these operations.
Probably the best solution is to use a default value when you create the column:
alter table table1 add RawSource varchar(255) not null default 'DTP';
If you don't want the default moving forward, you can remove it after the column is added.
Another method uses computed columns, but basically does the same thing:
alter table table1 add _RawSource varchar(255);
alter table1 add RawSource as (coalesce(_RawSource, 'DTP'));
at the time of addition of column to table only we can set a default value which will applies for all rows
Note:U should keep not null compulsory because if not all rows are applicable with nulls
alter table table_name
add row_source nvarchar(5) not null default(N'DTP')

Common methods for doing select with computation by need?

I would like to be able to add columns to a table with cells who's values are computed by need at 'querytime' when (possibly) selecting over them.
Are there some established ways of doing this?
EDIT: Okay I can do without the 'add columns'. What I want is to make a select query which searches some (if they exist) rows with all needed values computed (some function) and also fills in some of the rows which does not have all needed values computed. So each query would do it's part in extending the data a bit.
(Some columns would start out as null values or similar)
I guess I'll do the extending part first and the query after
You use select expression, especially if you don't plan to store the calculation results, or they are dependant on more than one table. An example, as simple as it could be:
SELECT id, (id+1) as next_id FROM table;
What type of database are you asking for? If it is SQL Server then you can use the computed columns by using the AS syntax.
Eg:
create table Test
(
Id int identity(1,1),
col1 varchar(2) default 'NO',
col2 as col1 + ' - Why?'
)
go
insert into Test
default values
go
select * from Test
drop table Test
In the SQL world it's usually expensive to add a column to an existing table so I'd advise against it. Maybe you can manage with something like this:
SELECT OrderID,
ProductID,
UnitPrice*Quantity AS "Regular Price",
UnitPrice*Quantity-UnitPrice*Quantity*Discount AS "Price After Discount"
FROM order_details;
If you really insist on adding a new column, you could go for something like (not tested):
ALTER TABLE order_details ADD column_name datatype
UPDATE order_details SET column_name = UnitPrice+1
You basically ALTER TABLE to add the new column, then perform an UPDATE operation on all the table to set the value of the newly added column.

update column using where clause a same field's multipul values

Dear all,
i need to update a table using same filed's multiple value.
Let: update test_table set column1=123 where column2=100,200,300......
I mean column 2 have multiple values.Now how i write the query??
Please help me.
try
update test_table set column1=123 where column2 IN(100,200,300)
look here for a tutorial:
http://www.webdevelopersnotes.com/tutorials/sql/tutorial_mysql_in_and_between.php3
If you mean that the match should happen where column2's value is one of the items in your list, use:
UPDATE test_table
SET column1=123
WHERE column2 IN (100,200,300, ...)
use FIND_IN_SET
FIND_IN_SET("id",test_table.column2)

I increment numerical value if there is same record , or insert wants to do 0 if there is not it

Please help me.
select * from hoge where id=xxx;
If there is a data, I do it to do it, but though I implement this step, there is if in a program, and insert hoge set data=0 is troublesome if update hoge set data=data+1, a result are 0 lines.
May not you realize this procedure by a blow by SQL?
replace hoge select id, data+1 as data from hoge where id = x;
When it was this SQL, a result was not usable because data did not enter in the case of NULL.
After all will not there be it whether it is a plural number or a comb by SQL in an if sentence?
If there is a simpler method, please teach it.
People thanking you in advance.
If I'm understanding the question properly (I don't think the OP is a native English speaker), you can use ON DUPLICATE KEY to do this in MySQL.
INSERT INTO table
(column1, column2, ...)
VALUES
('initial value for column1', 'initial value for column2', ...)
ON DUPLICATE KEY UPDATE
column1 = column1 + 1, column2 = 'new value for column2';
You seem to be asking how to create a row if it doesn't already exist or, if it does exist, update one of the fields in it.
The normal approach (for DBMS' that don't provide some form of UPSERT statement) is:
insert into TBL(keycol,countcol) values (key,-1)
update TBL set countcol = countcol + 1 where keycol = key
You need to ignore any errors on the first line (which should happen if the row already exists). Then the second statement will update it.
It's also unusual to initially insert a zero entry since you're generally adding one 'thing' the first time you do it. In that case, the insert would set the value to 0, not -1.