show all tables in DB2 using the LIST command - sql

This is embarrassing, but I can't seem to find a way to list the names of the tables in our DB2 database. Here is what I tried:
root#VO11555:~# su - db2inst1
root#VO11555:~# . ~db2inst1/sqllib/db2profile
root#VO11555:~# LIST ACTIVE DATABASES
We receive this error: SQL1092N "ROOT" does not have the authority to perform the requested command or operation.
The DB2 version number follows.
root#VO11555:~# db2level
DB21085I Instance "db2inst1" uses "64" bits and DB2 code release "SQL09071"
with level identifier "08020107".
Informational tokens are "DB2 v9.7.0.1", "s091114", "IP23034", and Fix Pack
"1".
Product is installed at "/opt/db2V9.7".

To get a list of tables for the current database in DB2 -->
Connect to the database:
db2 connect to DATABASENAME user USER using PASSWORD
Run this query:
db2 LIST TABLES
This is the equivalent of SHOW TABLES in MySQL.
You may need to execute 'set schema myschema' to the correct schema before you run the list tables command. By default upon login your schema is the same as your username - which often won't contain any tables. You can use 'values current schema' to check what schema you're currently set to.

Connect to the database:
db2 connect to <database-name>
List all tables:
db2 list tables for all
To list all tables in selected schema, use:
db2 list tables for schema <schema-name>
To describe a table, type:
db2 describe table <table-schema.table-name>
credit http://onewebsql.com/blog/list-all-tables

select * from syscat.tables where type = 'T'
you may want to restrict the query to your tabschema

I'm using db2 7.1 and SQuirrel. This is the only query that worked for me.
select * from SYSIBM.tables where table_schema = 'my_schema' and table_type = 'BASE TABLE';

Run this command line on your preferred shell session:
db2 "select tabname from syscat.tables where owner = 'DB2INST1'"
Maybe you'd like to modify the owner name, and need to check the list of current owners?
db2 "select distinct owner from syscat.tables"

have you installed a user db2inst2, i think, i remember, that db2inst1 is very administrative

Related

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

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>'));

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 to change hive table owner

I have a hive table 'sample' created with owner as 'X'
hive> show table extended like sample;
--shows owner as 'X'
Is there a way I can change the owner to some other 'Y', without recreating the table (don't want to lose the data)
A known option is to update the owner directly in the postgres hive metastore table.
hive=# update "TBLS" set "OWNER" = 'Y' where "OWNER" = 'X' and "TBL_NAME" = 'sample';
Is this safe?
As I know there is no other way to do it besides direct change in metastore DB. I needed to do this couple of times - there were no any issues.
I just experienced this issue and can share my resolution notes, to add color to what a "Direct Change in The Metastore DB" for me. In our configuration, we're using Presto that connects to Hive. Tables will be created in hive with whatever user that Presto connects as (with the --user flag on the Presto CLI).
We were getting an error message such as:
Access Denied: Cannot drop table SCHEMA.TABLE_NAME: Owner of the table is
different from session user
I can see the users of the table by executing the following query on the Hive Metastore:
select t.OWNER, p.PRINCIPAL_NAME, count(1)
from TBLS t
join TBL_PRIVS p on p.TBL_ID=t.TBL_ID
group by t.OWNER, p.PRINCIPAL_NAME;
And then, I can update the tables as needed by executing:
update TBLS set OWNER='NEW_OWNER' where OWNER='OLD_OWNER';
update TBL_PRIVS set PRINCIPAL_NAME='NEW_OWNER' where PRINCIPAL_NAME='OLD_OWNER';
NOTE: You should run this in a transaction and make sure your Metastore is backed up first.

SELECT data from another schema in oracle

I want to execute a query that selects data from a different schema than the one specified in the DB connection (same Oracle server, same database, different schema)
I have an python app talking to an Oracle server. It opens a connection to database (server/schema) A, and executes select queries to tables inside that database.
I've tried the following :
select ....
from pct.pi_int, pct.pi_ma, pct.pi_es
where ...
But I get:
ORA-00942: table or view does not exist
I've also tried surrounding the schema name with brackets:
from [PCT].pi_int, [PCT].pi_ma, [PCAT].pi_es
I get:
ORA-00903: invalid table name
The queries are executed using the cx_Oracle python module from inside a Django app.
Can this be done or should I make a new db connection?
Does the user that you are using to connect to the database (user A in this example) have SELECT access on the objects in the PCT schema? Assuming that A does not have this access, you would get the "table or view does not exist" error.
Most likely, you need your DBA to grant user A access to whatever tables in the PCT schema that you need. Something like
GRANT SELECT ON pct.pi_int
TO a;
Once that is done, you should be able to refer to the objects in the PCT schema using the syntax pct.pi_int as you demonstrated initially in your question. The bracket syntax approach will not work.
In addition to grants, you can try creating synonyms. It will avoid the need for specifying the table owner schema every time.
From the connecting schema:
CREATE SYNONYM pi_int FOR pct.pi_int;
Then you can query pi_int as:
SELECT * FROM pi_int;
Depending on the schema/account you are using to connect to the database, I would suspect you are missing a grant to the account you are using to connect to the database.
Connect as PCT account in the database, then grant the account you are using select access for the table.
grant select on pi_int to Account_used_to_connect

Why are tables created with default schema dbo although I specified a different schema?

If I run a sql script in SQL Server 2005 SSMS (Version 9.00.4035.00) like
CREATE TABLE xxx.MyTable
the table will be created as dbo.MyTable although the schema xxx does exist! No error message!
The user I'm using to run the script as all permissions (tested with windows user and sql user with server role sysadmin)
What's wrong?
You probably have 2 tables now
xxx.MyTable
dbo.MyTable
To check:
SELECT SCHEMA_NAME(schema_id), name, create_date, modify_date
FROM sys.objects
WHERE name = 'MyTable'
Don't rely on SSMS Object Explorer: it needs refreshed (right click on the tables node, refresh).
Or wrong database, wrong server etc.
We use schemas and never had any problems
Edit: now check all databases
EXEC sp_msforeachdb '
USE ?
SELECT SCHEMA_NAME(schema_id), name, create_date, modify_date
FROM sys.objects
WHERE name = ''MyTable''
'
Please take a look at the possible workarounds:
1) Create a SQL login with dbo rights to the database where tables and other objects have to be created. Have the users connect to SSMS using the SQL login that you have created. Tables can be created using SSMS without issues.
2) Have the user of windows security group create table using TSQL. You will see that a new schema and user will be created for this database with the user name of the user. Table gets created with windows user name as the owner .
Now, go to the database user which got created. Change the default schema to xxx.
User of that security group can create tables in SSMS and with dbo as the object owner.
Apparently, this is a microsoft bug and has not been resolved yet.
https://connect.microsoft.com/feedback/viewfeedback.aspx?FeedbackID=238246&wa=wsignin1.0&siteid=68
Hope this helps.