Insert data to table column from column of different table - oracle

I am using Oracle Database.
I have view called VW_MREQ
It has column as follows :
M_Product_ID
AD_Client_ID
AD_ORG_ID
It has records inside.
Then, I have empty table called M_Requisition
It has column as follows :
M_Product_ID
AD_Client_ID
AD_ORG_ID
DESCRIPTION
CREATEDBY
I am making Procedure and would like to insert Data manually to M_Requisition, the foreign key is M_Product_ID and I want the AD_Client_ID and AD_ORG_ID to be the same as in VM_REQ as I insert M_Product_ID to M_Requisition manually.
INSERT INTO M_Requisition(M_Product_ID, AD_Client_ID, AD_ORG_ID, DESCRIPTION, CREATEDBY) VALUES(123, ?? , ??,"Insert Data","Me")
I plan to use SELECT INTO but still confused how I arrange it as I am newbie in Oracle.
Your help will be useful.

You could use the insert-select syntax, and just query the hardcoded values as literals:
INSERT INTO M_Requisition
(M_Product_ID, AD_Client_ID, AD_ORG_ID, DESCRIPTION, CREATEDBY)
(SELECT 123, AD_Client_ID, AD_ORG_ID, 'Insert Data', 'Me'
FROM VW_MREQ)

Related

PL\SQL - Fetch values from table and Item - Oracle Apex

I need to insert data of one table to another table. All the values are from the table except one SO_ID. It is coming from the Item on the page. How do I do it?
insert into T_SORDER_ITEM_INWARD
(
select sd.ID, SO_ID
into :P25_SO_ID, sd.STOCK_ID,sd.ITEM_ID,sd.UOM_ID,
sd.ITEM_CONDITION_ID,sd.ORIGINAL,sd.ACTUAL,sd.WIDTH,sd.LENGTH,sd.STOCKQTY,
sd.KANTA,sd.RATE,sd.PACKET, sd.LABEL_METER, sd.EXCESS_SHORT,sd.LOCATION_ID,
sd.CLIENT_INITIAL, sd.FIN_YEAR, sd.SERIAL_NO
from T_STOCK_DETAIL sd join t_stock_master sm
on sd.stock_id = sm.stock_id
where sm.customer_id = p25_customer
)
A simplified example:
insert into another_table (id, name, location)
select :P25_SO_ID,
t.name,
t.location
from this_table t
Always name all columns you're inserting into (first line in my example).
Your query is impossible to understand. Not just because syntax is wrong, but because we have no idea which column is supposed to get which value.

How do you insert data into complex data type "Struct" in Hive

I'm completely new to Hive and Stack Overflow. I'm trying to create a table with complex data type "STRUCT" and then populate it using INSERT INTO TABLE in Hive.
I'm using the following code:
CREATE TABLE struct_test
(
address STRUCT<
houseno: STRING
,streetname: STRING
,town: STRING
,postcode: STRING
>
);
INSERT INTO TABLE struct_test
SELECT NAMED_STRUCT('123', 'GoldStreet', London', W1a9JF') AS address
FROM dummy_table
LIMIT 1;
I get the following error:
Error while compiling statement: FAILED: semanticException [Error
10044]: Cannot insert into target because column number type are
different 'struct_test': Cannot convert column 0 from struct to
array>.
I was able to use similar code with success to create and populate a data type Array but am having difficulty with Struct. I've tried lots of code examples I've found online but none of them seem to work for me... I would really appreciate some help on this as I've been stuck on it for quite a while now! Thanks.
your sql error. you should use sql:
INSERT INTO TABLE struct_test
SELECT NAMED_STRUCT('houseno','123','streetname','GoldStreet', 'town','London', 'postcode','W1a9JF') AS address
FROM dummy_table LIMIT 1;
You can not insert complex data type directly in Hive.For inserting structs you have function named_struct. You need to create a dummy table with data that you want to be inserted in Structs column of desired table.
Like in your case create a dummy table
CREATE TABLE DUMMY ( houseno: STRING
,streetname: STRING
,town: STRING
,postcode: STRING);
Then to insert in desired table do
INSERT INTO struct_test SELECT named_struct('houseno',houseno,'streetname'
,streetname,'town',town,'postcode',postcode) from dummy;
No need to create any dummy table : just use command :
insert into struct_test
select named_struct("houseno","house_number","streetname","xxxy","town","town_name","postcode","postcode_name");
is Possible:
you must give the columns names in sentence from dummy or other table.
INSERT INTO TABLE struct_test
SELECT NAMED_STRUCT('houseno','123','streetname','GoldStreet', 'town','London', 'postcode','W1a9JF') AS address
FROM dummy
Or
INSERT INTO TABLE struct_test
SELECT NAMED_STRUCT('houseno',tb.col1,'streetname',tb.col2, 'town',tb.col3, 'postcode',tb.col4) AS address
FROM table1 as tb
CREATE TABLE IF NOT EXISTS sunil_table(
id INT,
name STRING,
address STRUCT<state:STRING,city:STRING,pincode:INT>)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '.';
INSERT INTO sunil_table 1,"name" SELECT named_struct(
"state","haryana","city","fbd","pincode",4500);???
how to insert both (normal and complex)data into table

how to use one sql insert data to two table?

I have two table,and they are connected by one field : B_ID of table A & id of table B.
I want to use sql to insert data to this two table.
how to write the insert sql ?
1,id in table B is auto-increment.
2,in a stupid way,I can insert data to table B first,and then select the id from table B,then add the id to table A as message_id.
You cannot insert data to multiple tables in one SQL statement. Just insert data first to B table and then table A. You could use RETURNING statement to get ID value and get rid of additional select statement between inserts.
See: https://oracle-base.com/articles/misc/dml-returning-into-clause
Have you heard about AFTER INSERT trigger? I think it is what you are looking for.
Something like this might do what you want:
CREATE OR REPLACE TRIGGER TableB_after_insert
AFTER INSERT
ON TableB
FOR EACH ROW
DECLARE
v_id int;
BEGIN
/*
* 1. Select your id from TableB
* 2. Insert data to TableA
*/
END;
/

sql statement to select and insert one value from one table rest from outside the tables

I have two db2 tables, table one contains rows of constant data with id as one of the columns. second table contains id column which is foreign key to id column in first table and has 3 more varchar columns.
I am trying to insert rows into second table, where entry into id col is based on a where clause and the remaining columns get values from outside.
table1 has columns id, t1c1, t1c2, t1c3
table2 has columns id, t2c1, t2c2, t2c3
to insert into table2, I am trying this query:
insert into table2 values
(select id from table1 where t1c2 like 'xxx', 'abc1','abc2');
I know it is something basic I am missing here.
Please help correcting this query.

How to insert init-data into a table in hive?

I wanted to insert some initial data into the table in hive, so I created below HQL,
INSERT OVERWRITE TABLE table PARTITION(dt='2014-06-26') SELECT 'key_sum' as key, '0' as value;
but it does not work.
There is another query like the above,
INSERT OVERWRITE TABLE table PARTITION(dt='2014-06-26') SELECT 'key_sum' as key, '0' as value FROM table limit 1;
But it also didn't work, as I see that the tables are empty.
How can I set the initial data into the table?
(There is the reason why I have to do self-join)
About first HQL it should have from clause, its missing so HQL failure,
INSERT OVERWRITE TABLE table PARTITION(dt='2014-06-26') SELECT 'key_sum' as key, '0' as value;
Regarding second HQL, from table should have atleast one row, so it can set the constant init values into your newly created table.
INSERT OVERWRITE TABLE table PARTITION(dt='2014-06-26') SELECT 'key_sum', '0' FROM table limit 1;
you can use any old hive table having data into it, and give a hit.
The following query works fine if we have already test table created in hive.
INSERT OVERWRITE TABLE test PARTITION(dt='2014-06-26') SELECT 'key_sum' as key, '0' as value FROM test;
I think the table which we perform insert should be created first.

Resources