Error says table doesn't exist in database, while it exists - sql

I have extracted list of names of tables in database using this sql query:
"SELECT table_name from information_schema.tables"
I got this list:
table_name
1 main_table
2 kp_table
3 ids_table
4 main_logs
Then i want to extract table ids_table:
"SELECT * from ids_table"
So desired result is to get that table, but i get this error:
Error: Failed to prepare query: ERROR: relation "ids_table" does not exist
LINE 1: SELECT * from ids_table
Why it happens? Why i get from first query its name, but then it tells me that it doesn't exist?

In this case, I would say that the most common issue is that you are looking at two different databases. That is probably not the cause in this case.
Another common possibility is that the schema is something unexpected, and you should be referencing the schema. For that, include the schema in the query:
select table_schema, table_name
from information_schema.tables
Another possibility are hidden characters, such as spaces. You can see if this is the problem by adding delimiters so check the names:
select '|' || table_name || '|'
from information_schema.tables

It looks like you are trying to select data from different schema, which by default owns by different user, but you have access to it. As this table is not created by user, which you are using for select * from ... , you must indicate schemas name before table name. Without schemas name it's trying to select date from users schema, but actually table are owned by completely different user.

Related

Is there a way to get all column names in a table for IBM Netezza? [duplicate]

Is there a query I can write to search all the column names for a particular database in Netezza?
Within the same database you can use the following query:
select *
from _v_odbc_columns1
where column_name like '%columnname%'
or a less Netezza specific query
select *
from information_schema.columns
where column_name like '%columnname%'
The important catalog views in netezza system are listed below
_V_USER: the user view gives information about the users in the netezza system.
_V_TABLE: the table view contains the list of tables created in the netezza performance system.
_V_RELATION_COLUMN: the relation column system catalog view contains the columns available in a table.
_V_TABLE_INDEX: this system catalog contains the information about the
indexes created on table. netezza does not support creating indexes on a table as of now.
_V_OBJECTS: lists the different objects like tables, view, functions etc. available in the netezza.
Example:
SELECT *
FROM _V_RELATION_COLUMN
WHERE
ATTNAME like '%GEO%' --SPECIFY COLUMN NAME
AND type = 'TABLE'
You would access something similar to an information_schema.
Column Name, %COW%', would use % as a wildcard...gathering any column that has 'COW' in the name
SELECT *
FROM _V_SYS_COLUMNS
WHERE
COLUMN_NAME like '%COW%'
AND TABLE_SCHEMA = 'DEV'
ORDER BY TABLE_NAME
;

How to select the same column from multiple table with almost same name in SQL?

I'd like to select a 2 column from lot of table.
Exemple
Table_2017-01
id name value
Table_2017-02
id name value
Table_2017-03
id name value
etc...
My Query would be
SELECT name, value
FROM Table_2017-01, Table_2017-02, Table_2017-03
But I'd like to know if something it's possible like
SELECT name, value FROM LIKE Table_%
I know this last query is not possible and it could be easier for me if a query exist for this problem as I can have a lot of table with just a part of the name different.
To select mutiple columns with the same name you have to the table as prefix like:
SELECT `Table_2017-01.name`, `Table_2017-02.name`, `Table_2017-03.name` FROM Table_2017-01,`Table_2017-02, Table_2017-03
If you want them in one Column, use Union.
(SELECT `Table_2017-01.name` from Table_2017-01)
Union
(SELECT `Table_2017-02.name` from Table_2017-02)
It depends on your database. Oracle f.e. has a data dictionary with all tables.
Because I know oracle better than others I use this for the example:
SELECT TABLE_NAME FROM ALL_TABLES WHERE TABLE_NAME LIKE 'Table_2017-%';
Now there are the following ways I sometimes use:
I build a list of selects and fire them into the database:
via Excel (not explained here)
via SQL
SELECT
'SELECT '''||TABLE_NAME||''' TABLE_NAME, NAME, VALUE FROM '||
TABLE_NAME
||' UNION '
FROM ALL_TABLES WHERE TABLE_NAME LIKE 'Table_2017-%';
Now you can copy the result to your database (dont forget to delete the last "UNION")
via program (python, Lazarus, c# .. whatever, same view is needed)
But pay attention: It is quick and dirty, the column names and types have to match.

How do I list all the column names in Netezza?

Is there a query I can write to search all the column names for a particular database in Netezza?
Within the same database you can use the following query:
select *
from _v_odbc_columns1
where column_name like '%columnname%'
or a less Netezza specific query
select *
from information_schema.columns
where column_name like '%columnname%'
The important catalog views in netezza system are listed below
_V_USER: the user view gives information about the users in the netezza system.
_V_TABLE: the table view contains the list of tables created in the netezza performance system.
_V_RELATION_COLUMN: the relation column system catalog view contains the columns available in a table.
_V_TABLE_INDEX: this system catalog contains the information about the
indexes created on table. netezza does not support creating indexes on a table as of now.
_V_OBJECTS: lists the different objects like tables, view, functions etc. available in the netezza.
Example:
SELECT *
FROM _V_RELATION_COLUMN
WHERE
ATTNAME like '%GEO%' --SPECIFY COLUMN NAME
AND type = 'TABLE'
You would access something similar to an information_schema.
Column Name, %COW%', would use % as a wildcard...gathering any column that has 'COW' in the name
SELECT *
FROM _V_SYS_COLUMNS
WHERE
COLUMN_NAME like '%COW%'
AND TABLE_SCHEMA = 'DEV'
ORDER BY TABLE_NAME
;

How to find the name of a table based upon a column name and then access said table

I have a column name "CustomerIDClass" and I need to find the table it's associated with within an entire Oracle database.
I've run this to determine the owner and name of the table where this column name appears:
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CustomerIDClass%';
and I'm getting this response:
I don't have enough reputation to post the image, so here's the link: http://i.imgur.com/a7rcKoA.png
I have no idea how to access this (BIN$Csew==) table. When I try to use it as a table name I get errors or messages saying that no rows were returned.
My main goal here is to write a simple statement that lets me search the database for the "CustomerIDClass" and view the table that contains this column name.
This table is in the recycle bin. You have to issue FLASHBACK TABLE "Customer1"."BIN$Csew==$0" TO BEFORE DROP command, given you have the appropriate privileges.
Doc: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9012.htm
Do note that in oracle the column names are stored in capital but you are using mixed case in your like statement therefore the select clause will not return any result
Try the below
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CUSTOMERIDCLASS%';

Selecting column names and table names of a select statement

How can I select the column name and table name from a SQL?
I tried something like this but it didn't work:
select column_name, table_name from (select * from users);
This might sound silly, but I have a list of different SQLs and I need to extract their columns and tables into a list. So some of the statements could me:
select username, password from users
select createdate from userlog
select * from dept
...
If I can select the column name and table name of a select statement, then I should get, say for the first statement, username and password for columns and users for table name. And createdate for column and userlog for table name in the second statement.
Then if it all works, I can then loop through the list of select statements and extract their column and table names.
The below query worked for Oracle database.
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
You can see more about information-schema
Edit:
You may try like this:
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME IN (SELECT ColumnName FROM users)
You need to parse the SQL statement so the SQL engine figures out the columns and datatypes of the columns that the statement returns.
How you do it best depends on what environment you are using. In some programming languages when you create a SqlPreparedStatement or OraCommand or whatever the object may be called, that object may have a metadata collection populated with column information after parsing.
If you are doing it in the database itself, parsing your statement with DBMS_SQL can get you the information you need. See Example 8 in the documentation at this link:
http://docs.oracle.com/database/121/ARPLS/d_sql.htm#ARPLS68205
--
Oh, and this gives you column names of the select statement. The table names I do not know of any way to get easily.