How to get count of tables present in a particular schema in snowflake? - sql

I am trying to find the count of tables present in CDC schema, I tried with information_schema but the information_schema is empty for me.

The following SQL would give you the required results:
use schema <schemaname>;
show tables;
The result shows the number of tables in the schema.

Note: The information schema in a particular database only displays the objects to which your current role in the session has access.
Please make sure that the role you are using has required access control privileges to view the database objects. If not, if you have access to high privilege roles like SYSADMIN/ACCOUNTADMIN you could use them when viewing metadata information in Snowflake.
You can try querying the INFORMATION_SCHEMA.TABLES View with a query :
select count(distinct TABLE_NAME)
from <database_name>.INFORMATION_SCHEMA.TABLES
where TABLE_CATALOG = <database_name>
and TABLE_SCHEMA = <schema_name>;
Alternatively, you can also use the SHOW TABLES command as follows to get the count.
show tables in schema <DATABASE_NAME>.<SCHEMA_NAME>;
select count(*) from table(result_scan('<last_query_id>'));

Related

How to list all selectable tables with current user in SAP HANA DB

I want to list all tables with current user using sql.
I know there are some way to get privileges, like EFFECTIVE_PRIVILEGES, but it only shows privileges which is granted by GRANT SELECT ~ on [TABLE] to [USERS].
You can query the schema information to get this - there's no SHOW TABLES or equivalent available.
Try:
SELECT TABLE_NAME FROM "SYS"."TABLES" WHERE SCHEMA_NAME='<database_name>'
which works for me.
You can also get Column Names in thsi way from the SYS.COLUMNS view

How can I list all tables existent in a Database Link (Oracle)?

Basically I have a Database link (Oracle) called mylink.domain, in this link we can access foundation information like name of them members and other general information.
I would like to list all table's name in this link but i don't know how to do that.
Thanks in advance
You can access the all_tables view through the dblink:
select owner, table_name
from all_tables#dblink
order by owner, table_name;
Selecting the content of ALL_TABLES dictionary view will list you all tables your user has access to. Generally, it is not always possible to get a list of tables you dont have permissions for - they just do not show up. If your user has the SELECT ANY DICTIONARY priviledge, you can select the content of DBA_TABLES, which will always list all tables existing in the database.
select table_name from all_tables#dblinkname;
This shows all tables your linked user has access to.
Where I got the answer from

in oracle SQL developer How do I display list of all tables from current user/schema

I m working in oracle SQL developer database where I want to display list of all tables from current user/schema
You see the list of tables as owned by the current schema user in the Tree view of the connection under "tables". If you login as sys, then you see sys owned (of course).
If you want to see the schema of a different user, you need to have select permissions on the tables (and maybe some more in SQL Developer). Then you can see the tables of the other schema under <Connection>/Other Users/<User>/Tables/*.
If you don't see tables there, then you need to check for Synonyms, global synonyms or views. Finally if none of them are showing the expected tables (and you are aure you logged into the correct instance and CDB) then there might be a different active default schema for your user active after logon (typical case of a logon trigger). In this case the statement from before applies: look under the user who owns them.
SQL Developer internally used the ALL_* and USER_ system views. For example your tables owned by you: select TABLE_NAME from user_tables. More complete description of that is here.

PostgreSQL Views Query

I am new in writing queries. I need a list of views that every user can write queries to.
SELECT *
FROM Information_Schema.Views;
I tried that, but I guess it is a list of all views, but I need only those views that all users could write queries to.
The information about views is stored in information_schema.tables:
SELECT *
FROM Information_Schema.Tables
WHERE table_type = 'VIEW';
This will list each each users visible views (without the superusers.)
SELECT
usename, schemaname||'.'|| viewname as view
FROM
pg_views
,pg_user
WHERE
has_table_privilege (
pg_user.usename,
schemaname||'.'|| viewname,
'select'
)
AND
schemaname NOT IN (
'pg_catalog',
'information_schema'
)
AND usesuper=false
As far as listing a view that is visible to all users ... Postgres has fine grained permissions so there is no easy flag to search for. You would have to match that each view would produce a row of every user. But you could create a group for your users and then query for that.
select * from pg_catalog.pg_views
where schemaname NOT IN ('pg_catalog', 'information_schema')
order by schemaname, viewname;
will show you the viewname and its owner
If you are going to use the information schema, you need to be aware how it works. Per documentation:
The view views contains all views defined in the current database.
Only those views are shown that the current user has access to (by way
of being the owner or having some privilege).
Bold emphasis mine. This also provides a handy way of solving your problem. Run your query with a plain, new, non-superuser role that that wasn't granted membership in any other roles nor any direct privileges on any views (yet).
If you want to exclude system views add the WHERE clause:
SELECT *
FROM information_schema.views
WHERE table_schema NOT LIKE ALL ('{pg_%,information_schema}'::text[]);
You get only those views (and all of them), that public can access.
SQL Fiddle.
(Yes, use the query on information_schema.views you already had, no point in using information_schema.tables, like has been suggested.)
For more specific needs, I suggest you use the system catalogs instead. Actual privileges are stored in the system table pg_class in the column relacl. And the view pg_views lists all views, not just the ones the current role has privileges for.
Resolving actual privileges is not trivial. Use the dedicated "Access Privilege Inquiry Functions" like has_table_privilege() that #user17130 already suggested. Related answer:
How to check if a table exists in a given schema

Getting all tables from an oracle database

I'm trying to retrieve a list of tables from an Oracle connection. I'm not very familiar with Oracle terminology and thus, having hard time finding the information I need.
Right now I can use Microsoft Access to connect via ODBC and it pops up with a "Link Tables" dialog that lists all tables, not just the ones I "own". None of the queries I've tried so far, give me this data.
I'm trying "SELECT * FROM all_tables" but that doesn't show me the right data.
ALL_TABLES will show you all the tables that you have access to SELECT from. DBA_TABLES will show you all the tables that exist in the database though you'll need an additional privilege grant to be able to query the DBA* data dictionary objects.
Try select * from all_tables, that should do what you want.
It can be.. (If user has dba role)
select * from dba_tables
SELECT owner, table_name
FROM all_tables
You can also try
SELECT * FROM USER_TABLES
It will return list of tables owned by your user.
SELECT * FROM TAB; that will show you all the table and views