I Know of this below mentioned command for copying table fully but i am not able to figure out how this command will work because i cant just find the mentioning of newly created table. (courtesy SO).
SET LONG 5000
SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_OLD_TABLE_NAME' ) FROM DUAL;
And also i read in some oracle forum posts that this is not a one shot solution to copy table ,its data and all the constraints , triggers , indexes and other such objects.
IS this true ?
Simplest way to export is use pl/sql developer or sql developer and use export data functionality. They export entire table with constraints, indexes, data. Just give it a try.
I am not sure what you meant by "copy a table including DDL statements". If you are looking to create a new table using dbms_metadata.get_ddl, then the above command will actually need to be modified as (not tested) :-
Assuming that variable tbl_sql has been defined in plsql block having length of 32767, you can do :-
SELECT REPLACE(DBMS_LOB.SUBSTR(dbms_metadata.get_ddl('TABLE','MY_OLD_TABLE_NAME'),32760,1),'MY_OLD_TABLE_NAME','NEW_TABLE_NAME') INTO tbl_sql FROM DUAL;
EXECUTE IMMEDIATE tbl_sql;
If you want to create a new table, then the following might be even better :-
EXECUTE IMMEDIATE 'CREATE TABLE NEW_TABLE_NAME AS SELECT * FROM MY_OLD_TABLE_NAME';
The dbms_metadata.get_ddl statement will not copy your data. However, the second statement (CTAS) will copy your data also.
Both the statements do not copy the constraints (CTAS does copy NOT NULL constraints) and indexes (as far as I know). To copy indexes, you will have to execute the dbms_metadata.get_ddl statement passing 'INDEX' as the first argument.
SELECT REPLACE(DBMS_LOB.SUBSTR(dbms_metadata.get_ddl('INDEX','MY_OLD_INDEX_NAME'),32760,1),'MY_OLD_INDEX_NAME','NEW_INDEX_NAME') INTO idx_sql FROM DUAL;
EXECUTE IMMEDIATE idx_sql;
keep it simple
create table xyz_new as select * from xyz where 1=0;
Create table with indexes ( More useful )
create table xyz_new like xyz
Related
How do I drop a few tables (e.g. 1 - 3) using the output of a SELECT statement for the table names? This is probably standard SQL, but specifically I'm using Apache Impala SQL accessed via Apache Zeppelin.
So I have a table called tables_to_drop with a single column called "table_name". This will have one to a few entries in it, each with the name of another temporary table that was generated as the result of other processes. As part of my cleanup I need to drop these temporary tables whose names are listed in the "tables_to_drop" table.
Conceptually I was thinking of an SQL command like:
DROP TABLE (SELECT table_name FROM tables_to_drop);
or:
WITH subquery1 AS (SELECT table_name FROM tables_to_drop) DROP TABLE * FROM subquery1;
Neither of these work (syntax errors). Any ideas please?
even in standard sql this is not possible to do it the way you showed.
in standard sql usually you can use dynamic sql which impala doesn't support.
however you can write an impala script and run it in impala shell but it's going to be complicated for such task, I would prepare the drop statement using select and run it manually if this is one-time thing:
select concat('DROP TABLE IF EXISTS ',table_name) dropstatements
from tables_to_drop
I have so far figured out that to describe a table I can use the below:
select dbms_metadata.get_ddl('TABLE','<my table name>','<table owner>') from dual;
I also found that I can get a list of tables from the current user using the below statement:
select table_name from user_tables;
However I need to find a way to combine these two so I get a (preferably SQL file) output which basically describes all the tables in the current schema. How can I go about that?
Call dbms_metadata in your query on user_tables:
select dbms_metadata.get_ddl('TABLE',table_name,user)
from user_tables;
We have a table on the SQL Server 2008 which gets populated by various stored procedures. The problem is that the authors of these stored procedures used some poor choice in code and populated this table using the following syntax:
INSERT INTO persistant_table
SELECT *
FROM #temp_table_with_data
Basically they would create a #temp_table_with_data in the script and the columns would be in the same order and with the same name as they are in the persistant_table.
Now I need to add another column to this persistant_table, but if I do that, I will break all the stored procedures.
Is there a way for me to add a column to this table without breaking all the stored procedures? (In the long run, we will change the stored procedures).
Thank you
No I think.select * will pick all columns and column number should match.
I don't think it's big effort to change the line to have particular columns only or select statement to have default value for column or null and then * to store into columns sequentially. But at least 1 line to be changed
The "ALTER TABLE" is a SQL statement that allows you to make datatype changes to a database table (i.e. change datatype as well as Size columns from an existing table).
ALTER TABLE TableName ALTER COLUMN ColumnName NVARCHAR(200)
You cannot do it without affecting old scripts. This is why 'SELECT *' is not good practice. You'd better create new scripts with explicit column names like
SELECT column1, column2 ....
Okay, so I'm a novice at writing stored procedures. I'm trying to perform a function similar to a foreach() you would see in a programming language. Right now I have a temp table populated with the values I'd like to loop through. I would like to (for each value in this table) execute a SQL statement based upon that value. So, here's my pseudocode to illustrate what I'm really after here:
foreach(value in my temp table) {
SELECT * FROM TABLE WHERE column_x = value
}
No I know nothing of stored procedures so how can I get this done? Here's my script so far:
DROP TABLE SESSION.X;
CREATE GLOBAL TEMPORARY TABLE
SESSION.X (
TD_NAME CHAR(30)
);
INSERT INTO
SESSION.X
SELECT DISTINCT
TD_NAME
FROM
DBA.AFFIN_PROG_REPORT
WHERE
TD_NAME IS NOT NULL;
Any help is very much appreciated!
You need, by example, a cursor.
See the example: https://stackoverflow.com/a/4975012/3428749
See the documentation: https://msdn.microsoft.com/pt-br/library/ms180169(v=sql.120).aspx
This question already has answers here:
How to export query result to csv in Oracle SQL Developer?
(6 answers)
Closed 8 years ago.
I use an Oracle SQL Developer script to do a SELECT, displaying results in Query Results window. I then copy/paste the results into an Excel template for reporting.
I would like to replace the script with a PLSQL block, to allow looping etc. The problem is that simple SELECT FROM (without INTO) doesn't seem to work in PLSQL.
Is there any way to use PLSQL to display the results of a select in a window which I can copy/paste from?
Note: I am disallowed from using EXPORT to create text files directly, which would be much better than copy/paste. There is also a standard Oracle package that does output to a file directly from PLSQL, but I am disallowed from using it, too.
This post was marked as a duplicate of another post, one which asked how to get output from a SELECT that was NOT in a PL/SQL block. I do know how to do that and in fact it's what I am doing currently, as I mentioned in the OP. As I said, SELECT without INTO fails in PL/SQL.
You can create a temporary table:
CREATE GLOBAL TEMPORARY TABLE table_name (
( column1 datatype null/not null,
column2 datatype null/not null,
...
) ON COMMIT DELETE ROWS;
Then through each loop, you can insert your data in it:
INSERT INTO table_name
(SELECT statement);
Finally you can use select statement on temporary table to read data:
SELECT * FROM table_name
and then drop table:
drop table table_name;