Search code examples
oracle-databaseinsertprocedure

Insert data to table column from column of different table


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.


Solution

  • 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)