i have created a new column (exe. columnB) in my oracle table1, which i want to fill with values i get from the query:
select substr(acc_no, 10) from table1)
all the values i get from the query above i want to be inserted in columnB of table1.
Any help, how can i achive this? Thank you in advance
You would use update:
update table1
set columnB = substr(acc_no, 10);
You don't actually need to store the column. You can add it to the table as a virtual column:
alter table1 add columnB varchar2(255)
generated always as (substr(acc_no, 10)) virtual;
Related
I need help to write a SQL query that will replace values in one column by another column values in the same table. For example, given the following table, I want to replace values in column 2 by values in column 1. I think the "UPDATE table SET ..." clause would help but don't know how to use it. Can any one help me, please ?
enter image description here
UPDATE table t1
SET column1 = (SELECT column2 FROM table t2 WHERE t1.column1 = t2.column1);
I am trying to do an insert on a table from another table with matched values and IDs.
lets say on table1 there are values of names and IDS. Like John and 55.
I am trying to only insert the 55 into table2 since John is already on table2 but just missing his ID.
I know I can do update statement to update the value for John to 55, but my tables have over 3000 values, and it will be hard to do one at a time:
Anyway I can write a query to enter a value into the other table as long as the names match together?
what I tried so far:
insert into desired_table (id,version,source_id,description,r_id)
SELECT HI_SEQUENCE.nextval,'0', select min (id)
from table
where name in (select name from table2 where table2_name is not null),
table2_name,
table2.r_id from table2 where name is not null;
Issue with this statement is it inserts multiple values, but it only inserts it into where the min ID is.
Anyway I can adjust this and have it pull more than one ID?
Use Merge statement (https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver15)
Merge into Table1 d
Using Table 2 s
on d.name=s.name
when matching then update
age=s.age
when not matching then insert
(col1, col2)
values (s.col1, s.col2);
You might want a trigger to automate the above task.
Create Trigger sample after insert on
Table1 for each row
Begin
Update table2 set table2.age = :NEW.AGE where
table2.id=:NEW.Id
END;
Got this working by generating insert statements and running them with insert all
I'm a new hive learner. My hive editor does not support update statements. I want to update a column in a table. Let's say I have a table like this.
Input: table_A
field_description
value
field1_name
Age
field2_name
Address
But I want to get something like follows. There are more than 100 rows in table_A
Expected output: table_out
field_description
value
field1
Age
field2
Address
How can I do this using common table expressions? Any suggestions are appreciated.
You can use the Insert overwrite method to achieve the desired result -
INSERT OVERWRITE TABLE table_A
SELECT SUBSTR(field_description, 1, INSTR(field_description, '_') - 1), value
FROM table_A;
I have Audit table with more than 50 columns and need to insert desired row (duplicate) by changing just one column value (column CreatedDate, set value to GETDATE()). I know this can be achieved by INSERT INTO SELECT * FROM but as there is more han 50 columns, code would seem messy:
INSERT INTO Audit_Table (Col1, Col2, .....CreatedDate, ......, Col50, ...ColN)
SELECT (Col1, Col2, .....GETDATE(), ......, Col50, ...ColN) FROM Audit_Table
WHERE Audit_Table.Id = Desired_Id
If i shouldn't change CreatedDate column value, it would be very simple:
INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id
Is there any other way to duplicate row and change only one/desired column value?
You can insert the record into a temporary table, update the CreatedDate column to GETDATE(), then insert it into the Audit_Table.
No. There is no way to say * except column_foo in SQL.
The workaround would be to generate the
SELECT
col1
, col2
, [...]
, coln
FROM foo;
statement (or parts of it) by querying the database's system catalogue for the column names in their order. There is always a table with all tables and a table with all columns.
Then, make sure you put the necessary commas in the right place (or remove them where you don't need them, or generate the comma in all rows of the report but the first - by using the ROW_NUMBER() OLAP function and evaluating whether it returns 1 or something else). Finally, edit the right date column, by replacing it with CURRENT_DATE or whatever your database uses for the current day.
Good luck -
Marco
You can build upon your existing idea. Just duplicate the row (I assume, you have an auto-incrementing primary key column) and then in a separate query update the time i.e.
Do this :
INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id
And then :
UPDATE Audit_Table SET CreatedDate = GETDATE() WHERE primaryKeyID = newPrimaryKeyID
Hope this helps!!!
try below as reference
you can use below statement to copy the all rows,
mysql> insert into newstudent select * from students;
you can use below statement to copy the specific row from table,
mysql> insert into newstudent
-> select id, name, age, address
-> from students
-> where
-> id = 1248;
you can use below statement to copy the either of the row from table,
mysql> insert into newstudent
-> select id, name, age, address
-> from students
-> where
-> id = 1248 or id=1249;
use limit clause also along with this
I am trying to come up with a DB2 SQL statement that does the following, but I am not sure
if this is allowed.
I know that it's possible to insert into tableA ( ... ) Values (?,?,?,...)
and then assign values to those parameters ?.
Is it possible to pre-defined the value of one of the parameter?
For example, one of the column that I am trying to insert is the ID column and I would like to make it something like select max(id) + 1 from tableA.
This is what I am trying to get to - is this syntax possible in db2?
insert into tableA (ID, Text1, Text2) VALUES (select max(id)+1 from tableA, ?, ?)
Anyways - any help would be appreciated!
thanks!!
this should works :
insert into tableA values((select max(id)+1 from tableA),'text1','text')
It sounds like you are wanting a primary key as an index on your table.
db2 alter table tableA add primary key (id)
This will create a column in your table which will auto-increment when you add a new record.
Source ( http://www.ibm.com/developerworks/data/library/techarticle/dm-0401melnyk/ )
You can also try using the OVERRIDING USER VALUE parameter:
INSERT INTO TableA
OVERRIDING USER VALUE
SELECT 0,Text1, Text2
From TableB