Insert table from one database into another database - sql

I want to copy a table to another database server in SQL developer.
I use this method to solve this problem.
INSERT INTO mahdi-jiradb..empier SELECT * FROM tamindb..users;
but i get an error:
Error starting at line : 8 in command -
INSERT INTO mahdi-jiradb..empier
SELECT * FROM tamindb..users
Error at Command Line : 8 Column : 18
Error report -
SQL Error: ORA-00926: missing VALUES keyword
00926. 00000 - "missing VALUES keyword"
*Cause:
*Action:
What should i do?

As you commented, it looks as if you use Oracle.
In that case, schema name can't contain a "minus" sign (mahdi-jiradb) so that's probably underline (mahdi_jiradb) instead.
SQL> create user mahdi-jiradb identified by test;
create user mahdi-jiradb identified by test
*
ERROR at line 1:
ORA-00922: missing or invalid option
Unless, of course, you enclosed username into double quotes (but now you have to use it always, everywhere):
SQL> create user "mahdi-jiradb" identified by test;
User created.
Presuming that that's not the case, schema name is wrong. Check it.
Also, use one dot to separate schema name from table name.
Something like this might work:
INSERT INTO mahdi_jiradb.empier SELECT * FROM tamindb.users;
presuming that both tables share the same description. That's why it is better to explicitly name all columns involved, e.g.
INSERT INTO mahdi_jiradb.empier (user_id, user_name)
SELECT uid, uname FROM tamindb.users;

Related

Moving a table from one schema to another in Exasol

I am trying to move a table which resides in a certain schema to a different schema with the same table name. I have tried the following but they do not work:
rename <OLD_SCHEMA_NAME>.<TABLE_NAME> TO <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: invalid identifier chain for new name [line 1, column 100] (Session: 1722923178259251200)
and
ALTER TABLE <OLD_SCHEMA_NAME>.<TABLE_NAME> RENAME <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: syntax error, unexpected IDENTIFIER_PART_, expecting COLUMN_ or CONSTRAINT_ [line 1, column 62] (Session: 1722923178259251200)
Many Thanks!
According to Exasol documentation there is no way to move table between schemas using RENAME statement:
Schema objects cannot be shifted to another schema with the RENAME
statement. For example, 'RENAME TABLE s1.t1 TO s2.t2' is not allowed.
I would move the table this way:
create table <NEW_SCHEMA_NAME>.<TABLE_NAME>
like <OLD_SCHEMA_NAME>.<TABLE_NAME>
including defaults
including identity
including comments;
insert into <NEW_SCHEMA_NAME>.<TABLE_NAME>
select *
from <OLD_SCHEMA_NAME>.<TABLE_NAME>;
drop table <OLD_SCHEMA_NAME>.<TABLE_NAME>;

Newly Added Column is not Appearing in Synonym

I have a table named YY_ZZ_VAT_TRX_DETAILS under the XX schema.
It has an existing SYNONYM to AA schema.
I am currently logged in as AA and I wanted to add a column to YY_ZZ_VAT_TRX_DETAILS and executed the below command just fine
alter table XX.YY_ZZ_VAT_TRX_DETAILS
add (USER_DEFINED_FISC_CLASS VARCHAR2(30));
Table XX.YY_ZZ_VAT_TRX_DETAILS altered.
I tried to select the Column from the Table using the simple query below
select USER_DEFINED_FISC_CLASS
from YY_ZZ_VAT_TRX_DETAILS;
But I surprisingly got this error:
ORA-00904: "USER_DEFINED_FISC_CLASS": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 1 Column: 9
I thought I just missed altering the table, so I re-ran the ALTER script again, but got this error instead:
Error starting at line : 4 in command - alter table
XX.YY_ZZ_VAT_TRX_DETAILS add (USER_DEFINED_FISC_CLASS VARCHAR2(30))
Error report - ORA-01430: column being added already exists in table
01430. 00000 - "column being added already exists in table"
*Cause:
*Action:
I checked the Object Definition and sure enough, I found the new column there:
But when I query all the columns from the Table, I still can't see it:
select *
from YY_ZZ_VAT_TRX_DETAILS;
However, when I query using the XX schema prefix, I'm able to see the column:
select *
from XX.YY_ZZ_VAT_TRX_DETAILS;
Why isn't the synonym picking up the newly-added column?
It seems this was caused by this "Edition-Based Redefinition" thing that Oracle recently introduced for 12c.
Upon further investigation, what happened was:
The Table XX.XX_ZZ_VAT_TRX_DETAILS has a Editionable view named XX_ZZ_VAT_TRX_DETAILS# that only selects a certain number of columns.
Now, the View XX_ZZ_VAT_TRX_DETAILS# has a SYNONYM named XX_ZZ_VAT_TRX_DETAILS under the schema AA, hence the confusion on the Table having a SYNONYM of the same name.
To resolve this, i modified the view XX_ZZ_VAT_TRX_DETAILS# and added the new column and successfully recompiled it.
Once this was done, the synonym now shows the newly-added column and I was able to compile the stored procedure successfully.
I believe you could be having a table or a view called YY_ZZ_VAT_TRX_DETAILS or a private synonym that points to another table?.
In oracle the preference while querying is
1)objects_in same schema
2)private synonym
3)public synonym

Can only find table in Oracle SQL when surrounding the table name with double quotes. Why?

I'm having the following weird issue with Oracle SQL. I have a table called schema_version, but I can't run simple selects over it:
> SELECT * FROM schema_version;
ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Error at Line: 1 Column: 15
Now if I attempt to run the very same query but this time surrounding the table name with double quotes everything seems to run fine:
> SELECT * FROM "schema_version";
< results are shown >
From what I've read # https://stackoverflow.com/a/7425931/130758 I should be okay not using double quotes. What may I be missing? Thanks
If the table was created as
CREATE TABLE "schema_version" (...)
its name is stored in the catalog in the exact (lower) case. When you reference the name unquoted:
SELECT * FROM schema_version;
the name is converted to the upper case by default, so the table cannot be found. When you reference the name quoted:
SELECT * FROM "schema_version";
the table name is used as is (in lower case) and matches that in the catalog.

A CREATE statement with quoted fields in Oracle

I have created a table in Oracle 10g using the following CREATE statement.
CREATE TABLE test ("id" NUMBER(35, 0) primary key, "description" VARCHAR2(250) not null);
The basic table structure looks like as follows.
--------------------------------------------------------------------------------
Column Name Data Type Nullable Default Primary Key
--------------------------------------------------------------------------------
id NUMBER(35, 0) No - 1
description VARCHAR2(250) No - -
It should precisely be noted that the column names in this CREATE statement are enclosed within double quotes just for having a fun :)
After issuing this DDL statement, I issued three DML statements to add this many rows as follows.
INSERT INTO test VALUES (1, 'aaa');
INSERT INTO test VALUES (2, 'bbb');
INSERT INTO test VALUES (3, 'ccc');
And finally, the following SELECT statement was executed to verify, if those rows were inserted.
SELECT * FROM test;
Oracle indeed displays three rows exactly as inserted on executing this query.
But when I issue the following SELECT query,
SELECT id, description FROM test;
Oracle complains,
ORA-00904: "DESCRIPTION": invalid identifier
The following (same) query also,
SELECT id FROM test;
fails with the error,
ORA-00904: "ID": invalid identifier
The same is true for the query,
SELECT description FROM test;
The only SELECT query with the meta character * works. Listing fields in the SELECT clause doesn't work. Capitalizing the column names in the SELECT clause also doesn't work.
What is the reason behind it?
Please don't just say, Don't do this. I'm interested in knowing the reason behind it.
OK, I won't say it, I'll just think it loudly.
The documentation clearly says that if you have quoted identifiers, you have to quote them everywhere (my italics for emphasis):
Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
So you always have to do:
SELECT "id", "description" FROM test;
Which is a pain. But obviously I'm just thinking that too, not really saying it.

SQL Error: ORA-00913: too many values

Two tables are identical in terms of table name, column names, datatype and size. These tables are located in separate databases, but I am use to
current Log in in hr user.
insert into abc.employees select * from employees where employee_id=100;
I can not give use original query from corporate office.
Error starting at line 1 in command:
insert into abc.employees select * from employees where employee_id=100;
Error at Command Line:1 Column:25
Error report:
SQL Error: ORA-00913: too many values
00913. 00000 - "too many values"
*Cause:
*Action:
You should specify column names as below. It's good practice and probably solve your problem
insert into abc.employees (col1,col2)
select col1,col2 from employees where employee_id=100;
EDIT:
As you said employees has 112 columns (sic!) try to run below select to compare both tables' columns
select *
from ALL_TAB_COLUMNS ATC1
left join ALL_TAB_COLUMNS ATC2 on ATC1.COLUMN_NAME = ATC1.COLUMN_NAME
and ATC1.owner = UPPER('2nd owner')
where ATC1.owner = UPPER('abc')
and ATC2.COLUMN_NAME is null
AND ATC1.TABLE_NAME = 'employees'
and than you should upgrade your tables to have the same structure.
The 00947 message indicates that the record which you are trying to send to Oracle lacks one or more of the columns which was included at the time the table was created.
The 00913 message indicates that the record which you are trying to send to Oracle includes more columns than were included at the time the table was created.
You just need to check the number of columns and its type in both the tables
ie the tables that are involved in the sql.
If you are having 112 columns in one single table and you would like to insert data from source table, you could do as
create table employees as select * from source_employees where employee_id=100;
Or from sqlplus do as
copy from source_schema/password insert employees using select * from
source_employees where employee_id=100;
For me this works perfect
insert into oehr.employees select * from employees where employee_id=99
I am not sure why you get error. The nature of the error code you have produced is the columns didn't match.
One good approach will be to use the answer #Parodo specified
this is a bit late.. but i have seen this problem occurs when you want to insert or delete one line from/to DB but u put/pull more than one line or more than one value ,
E.g:
you want to delete one line from DB with a specific value such as id of an item but you've queried a list of ids then you will encounter the same exception message.
regards.