3

Currently I have the following SELECT statement:

CREATE TABLE  TEST AS
SELECT ROW_ID,
            PROM_INTEG_ID,
            INTEGRATION_ID,
            BILL_ACCNT_ID,
            SERV_ACCT_ID,
            CFG_STATE_CD   
FROM PRODUCTS
WHERE PROD_ID = 'TestProduct'
AND STATUS_CD = 'Active';

However I have to add some additional columns which do not exist in the PRODUCTS table and define them with my own name .e.g HIERARCHY

I tried using the WITH operand in my SQL query but it keeps failing as the syntax is wrong.

    CREATE TABLE  TEST AS
SELECT ROW_ID,
            PROM_INTEG_ID,
            INTEGRATION_ID,
            BILL_ACCNT_ID,
            SERV_ACCT_ID,
            CFG_STATE_CD   
            WITH
                  PRODUCT_HEIRARCHY varchar2(30)  'Test123Value'
FROM PRODUCT
WHERE PROD_ID = 'TestProduct'
AND STATUS_CD = 'Active';

So in summary, I want to pull in columns from an existing table as well as defining some of my own.

Any help appreciated

0

2 Answers 2

9

Just add the columns to the select:

CREATE TABLE TEST AS 
    SELECT ROW_ID, PROM_INTEG_ID, INTEGRATION_ID, BILL_ACCNT_ID, SERV_ACCT_ID, CFG_STATE_CD,
           CAST('Test123Value' AS VARCHAR2(30)) as PRODUCT_HIERARCHY
    FROM PRODUCTS
    WHERE PROD_ID = 'TestProduct' AND STATUS_CD = 'Active';

Note that the cast() is not necessary. But it is a good idea if you want the column to have a specific type.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this, out of curiosity is the WITH operator for multiple table SELECTS?
What if we want all columns from the original table ? SELECT *, CAST('Test123Value' AS VARCHAR2(30)) as PRODUCT_HIERARCHY does not seem to work (ORA-00923: FROM keyword not found where expected)
Answer (I am new to oracle database) - we can't use the * alone we need the table name: stackoverflow.com/questions/9727018/…
@Mr_and_Mrs_D . . . You would qualify the *: products.*.
2

Also using CTE i.e. WITH clause as known commonly, you could create table.

CREATE TABLE t
AS 
WITH data AS ( 
   SELECT...
) 
SELECT * 
FROM data

3 Comments

Could you spell the query so it answers the question ?
@Mr_and_Mrs_D The OP has not posted the details of the rest of the columns and tables he want to add to the select. So, I assume he has to JOIN the other tables and refer them in the column list using the table alias. All that will go inside the WITH clause here.
Cf. the answer by @Gordon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.