Create table sql in Oracle which prompts for table name - sql

I want to create table so that when the query runs, it asks for the table name. I am sure this is a duplicate post but would like to know if this is possible with Oracle?

If you are using SQL*Plus, you can use substitution variables (&variable_name)
SQL> create table &name (
2 col1 number,
3 col2 date
4 );
Enter value for name: fuzzy_bunny
old 1: create table &name (
new 1: create table fuzzy_bunny (
Table created.
SQL> select * from fuzzy_bunny;
no rows selected

Related

Oracle problem creating index organized table using "create as" statement

So I'm trying to create a copy of one of my tables into an index organized table, However I get an error, here is the code.
create table clients2 as
select *
from clients
organization index;
ORA-00933:"SQL command not properly ended"
Your command is wrong.
SQL> create table testiot ( object_id primary key,object_name )
organization index
as select object_id,object_name from dba_objects
where object_id is not null ;
Table created.
SQL> select count(*) from testiot ;
COUNT(*)
----------
208730
Organization index must be in the definition of the table and before as select. On the other hand, you need to define the columns in the table and which one is the primary key for IOT.

DROP TABLE by CONCAT table name with VALUE from another SELECT [SQLite]

I was wondering how can I drop table with concat by selecting value from other table.
This is what I am trying to figure out:
DROP TABLE SELECT 'table' || (select value from IncrementTable)
So basically table name is table6 for example.
Goal is: eg.. DROP TABLE table6
You can't do this directly. Table and column names have to be known when the statement is byte-compiled; they can't be generated at runtime. You have to figure out the table name and generate the appropriate statement string in the program using the database, and execute it.

Is there a way to create a temporary table in SQL that deletes right after the query finishes? [duplicate]

This question already has answers here:
Creating temporary tables in SQL
(2 answers)
Closed 6 years ago.
I have a complicated query I'm working on. It involves several tables.
It would be very helpful for me to create a new table and then simply query from that. However, this is a shared database and I don't want to make a new table, especially when i don't plan on using that table specifically. (I just want it as a stepping stone in my query)
Is it possible to create a table just for 1 query that deletes right when the query is done? (i.e a temporary table)
Sure. Use CREATE TEMPORARY TABLE:
=> CREATE TEMPORARY TABLE secret_table(id BIGSERIAL, name text);
=> INSERT INTO secret_table(name) VALUES ('Good day');
INSERT 0 1
=> INSERT INTO secret_table(name) VALUES ('Good night');
INSERT 0 1
=> SELECT * FROM secret_table;
id | name
----+------------
1 | Good day
2 | Good night
(2 rows)
But upon reconnection:
psql (9.5.4)
Type "help" for help.
=> SELECT * FROM secret_table;
ERROR: relation "secret_table" does not exist
LINE 1: SELECT * FROM secret_table;
You could use temporary tables which drops itself at the end of session in which they were created (not after the query finishes, as you've said). Though, you could always drop it manually at the end of your operation.
If you'd like to create such table as a result from a query then this is the sample to be expanded to your needs:
CREATE TEMP TABLE tmp_table_name AS ( SELECT 1 AS col1 );
But I'm thinking you may be looking for a CTE instead of a table since you're saying that you're planning to use it only once. Consider this:
WITH tmp_table AS ( SELECT 1 AS col1 )
SELECT *
FROM tmp_table
...
You can also do dinamically The result of a query is also a Table
select * from (select col1, col2, col3
from my_complex_table
... ) t1
use keyword temporary, the temporary table is only visible in your current connection and drop after you disconnect your connection.
The other way would create a table and drop the table by yourself when you don't need it

sql query to display unavailable number?

i have some list of numbers with me, i want to check that numbers in a specified table available or not, if any number is not available in the table from the list of given numbers, then that unavailable number need to be display
I didnt sure that i understad your query correctly,
but it seems you mean somthing like this:
select *
from TABLE_NAME t
where t.COLUMN_NAME not in (1,2,3.... (#your list values#))
save the list of number first:
Create table list(id int);
insert into list(id)values(1);
insert into list(id)values(2);
insert into list(id)values(3);
insert into list(id)values(4);
Create sample table
Create table chck_list(id int,name varchar2(10));
insert into chck_list(id,name)values(1,'Micheal');
insert into chck_list(id,name)values(2,'John');
insert into chck_list(id,name)values(8,'Jack');
Query to check if there are id in list not showed in chck_list
select list.id
from list lst
where not exists(select 1 from chck_list chck where chck.id=lst.id)
Result
=========================
id
=========================
3
4

create table in hive with additional columns

I am new to Hive . I want to create the table in hive with the same columns as that of existing table plus some additional columns. I Know we can use something like this.
CREATE TABLE new_table_name
AS
SELECT *
FROM old_table_name
This will create the table with same columns as that of old_table_name.
But How do I specify additional columns in new_table_name ?
Here is how you can achieve it:
Old table:
hive> describe departments;
OK
department_id int from deserializer
department_name string from deserializer
Create table:
create table ctas as
select department_id, department_name,
cast(null as int) as col_null
from departments;
Displaying Structure of new table:
hive> describe ctas;
OK
department_id int
department_name string
col_null int
Time taken: 0.106 seconds, Fetched: 3 row(s)
Results from new table:
hive> select * from ctas;
OK
2 Fitness NULL
3 Footwear NULL
4 Apparel NULL
5 Golf NULL
6 Outdoors NULL
7 Fan Shop NULL
8 TESTING NULL
8000 TESTING NULL
9000 testing export NULL
Simple way is to issue ALTER TABLE command to add more(additional) columns after the above CREATE statement.
first create a new table like the first one
and after that alter this new table and add the columns that you want.
CREATE TABLE new_table LIKE old_table;
ALTER TABLE new_table ADD COLUMNS (newCol1 int,newCol2 int);
if you wish to avoid data copy, make your table external
I wish that it helps you :)