Get full "path" of SQL tables with R DBI package - sql

I'm new to SQL so I don't know the correct wordings, sorry for that.
When I establish a connection with dbConnect of the DBI package, I can list all tables of the database:
> head(dbListTables(conn))
[1] "cdw_apps" "cdw_attachments"
[3] "cdw_auditLogs" "cdw_blueprints"
[5] "cdw_businessObjects" "cdw_businessObjectsActions"
However I can't access directly to these tables, they are nested (in some kind of "folders", I don't know the wording, again).
For example I have to do:
dbGetQuery(
conn,
"select top 100 * from [vxda-prod-sqldw01].[vxrd_cdw].[cdw_apps]"
)
Is it possible to list the tables with their "path" (I mean [vxda-prod-sqldw01].[vxrd_cdw].[cdw_apps] and not only cdw_apps)?

Most databases support information_schema.tables (and .columns, .routines, and others I believe), and SQL Server is among them. While there are newer object-discovery tables/mechanisms specifically in T-SQL, these still work and are sufficient for this need.
For instance,
DBI::dbGetQuery(conn, "select * from information_schema.tables")
# TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
# 1 mycatalog dbo table1 BASE TABLE
# 2 mycatalog dbo table2 BASE TABLE
# 3 mycatalog dbo someview VIEW
will return all tables (and views) visible.

Related

Problem to data access from sql db by using r studio

I can able to connect DB by using following code
library(RODBC)
library(DBI)
library(odbc)
conn3 <- DBI::dbConnect(odbc :: odbc(),
Driver = 'SQL Server',
Server = 'xxx',
Database = 'xxx',
UID = 'xxx',
Pwd = 'xxx',
TrustServerCertificate='no',
#trusted_connection = 'yes',
Port = 1433
)
And i can able to see the DB data table.
But when i try to access the data by using following code
myquery <- dbSendQuery(conn3, "SELECT * FROM mhealthpbi1.report.blood_test")
df<-dbFetch(myquery)
It showws the following error
Error in result_fetch(res#ptr, n) :
nanodbc/nanodbc.cpp:3069: 07009: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index
and show the error message (image attached)
can anyone help me to access or extract data?
can anyone help me to access or extract data by providing code or explain process?
Try myquery <- dbSendQuery(conn3, "SELECT * FROM mhealthpbi1.report.blood_test;") or make sure the table name is correct, remember that you have already selected the database in dbConnect.
Example for mySQL:
mysqlconnection = dbConnect(RMySQL::MySQL(),
dbname='cars',
host='localhost',
port=3306,
user='xxx',
password='xxx')
brand = fetch(dbSendQuery(mysqlconnection, "select brand from sale_2018"))
In my experience, this error can be caused by a MS ODBC-driver (ahem) "feature": all long columns (ill-bounded constraint) must be at the end of the list of columns1. I'm inferring that you have an odbc version older than 1.3.1, since it was sufficiently mitigated in that release.
Two options:
See if/which columns are "large", often created as nvarchar(max) or blob:
fields <-
DBI::dbGetQuery(conn3,
"select TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
from information_schema.columns
where TABLE_NAME='blood_test'")
Look at fields, and if character_maximum_length is over -1 (max) or possibly a high number over 255 or so, then that column should be at the end. In this case, you'll need to change from select * to specific columns. (Many SQL gurus discourage select * anyway, for reasons relating to defensive programming and unsupervised/automated queries.)
If you really "need" select *, then I suggest you write a sql VIEW that orders the columns correctly, and then allows one to select *. Crafting this is rather simple assuming that the user permissions in the database allows it. If so, then it may be as simple as
CREATE VIEW myview AS
select column1, column3, column5,
bigcolumn2, bigcolumn4
from mhealthpbi1.report.blood_test
and then subsequently use select * from myview.
Update your version of odbc-1.3.1 or newer (1.3.3 is current as of this writing), as it should resolve the issue.
Notes:
https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/getting-long-data

DB2 Query to retrieve all table names for a given schema

I'm just looking for a simple query to select all the table names for a given schema.
For example, our DB has over 100 tables and I need to find any table that contains the sub-string “CUR”. I can use the like command once I have all the tables.
--for DB2/z
select * from sysibm.systables
where owner = 'SCHEMA'
and name like '%CUR%'
and type = 'T';
--for DB2/LUW
select * from sysibm.systables
where CREATOR = 'SCHEMA'
and name like '%CUR%'
and type = 'T';
This will give you all the tables with CUR in them in the SCHEMA schema.
See here for more details on the SYSIBM.SYSTABLES table. If you have a look at the navigation pane on the left, you can get all sorts of wonderful DB2 metatdata.
Note that this link is for the mainframe DB2/z. DB2/LUW (the Linux/UNIX/Windows one) has slightly different columns, as per the second query above.
You should examine the IBM docs for your specific variant if you're using neither of those.
DB2 LIST TABLES FOR SCHEMA <schema_name>
On my iSeries I have to run this command from iNavigator:
select *
from QSYS2.SYSTABLES
where TABLE_SCHEMA
like 'SCHEMA_NAME'
and TYPE = 'T';
You should try this:
select TABNAME from syscat.tables where tabschema = 'yourschemaname'";
Using the DB2 commands (no SQL) there is the possibility of executing
db2 LIST TABLES FOR ALL
This shows all the tables in all the schemas in the database.
ref: show all tables in DB2 using the LIST command
For Db2 for Linux, Unix and Windows (i.e. Db2 LUW) or for Db2 Warehouse use the SYSCAT.TABLES catalog view. E.g.
SELECT TABSCHEMA, TABNAME FROM SYSCAT.TABLES WHERE TABSCHEMA LIKE '%CUR%' AND TYPE = 'T'
Which is a SQL statement that will return all standard tables in all schema that contains the substring CUR. From a Db2 command line you could also use a CLP command e.g. db2 list tables for all | grep CUR to similar effect
This page describes the columns in SYSCAT.TABLES including the different values for the TYPE column.
A = Alias
G = Created temporary table
H = Hierarchy table
L = Detached table
N = Nickname
S = Materialized query table
T = Table (untyped)
U = Typed table
V = View (untyped)
W = Typed view
Other commonly used catalog views include
SYSCAT.COLUMNS Lists the columns in each table, view and nickname
SYSCAT.VIEWS Full SQL text for view and materialized query tables
SYSCAT.KEYCOLUSE Column that are in PK, FK or Unique constraints
In Db2 LUW it is considered bad practice to use the SYSIBM catalog tables (which the SYSCAT catalog views select their data from). They are less consistent as far as column names go, are not quite as easy to use, are not documented and are more likely to change between versions.
This page has a list of all the catalog views Road map to the catalog views
Also, for a general set of queries against the Db2 catalog (and system functions) you can look at the IBM samples library in the db-library section
which includes views to do things like generate (approximate) DDL as well as many other things.
For Db2 for z/OS, use SYSIBM.TABLES which is described here. E.g.
SELECT CREATOR, NAME FROM SYSIBM.SYSTABLES WHERE OWNER LIKE '%CUR%' AND TYPE = 'T'
For Db2 for i (i.e. iSeries aka AS/400) use QSYS2.SYSTABLES which is described here
SELECT TABLE_OWNER, TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_SCHEMA LIKE '%CUR%' AND TABLE_TYPE = 'T'
For DB2 Server for VSE and VM use SYSTEM.SYSCATALOG which is described here DB2 Server for VSE and VM SQL Reference
SELECT CREATOR, TNAME FROM SYSTEM.SYSCATALOG WHERE TABLETYPE = 'R'
db2 connect to MY_INSTACE_DB with myuser -- connect to db2
db2 "select TABNAME from syscat.tables where tabschema = 'mySchema' with ur"
db2 terminate -- end connection
select name from sysibm.systables
where name like '%ISP%'
and type = 'T'
You can also get the table names simply by typing LIST TABLES in DB2
This is my working solution:
select tabname as table_name
from syscat.tables
where tabschema = 'schema_name' -- put schema name here
and type = 'T'
order by tabname
This should work:
select * from syscat.tables
SELECT
name
FROM
SYSIBM.SYSTABLES
WHERE
type = 'T'
AND
creator = 'MySchema'
AND
name LIKE 'book_%';
There is no big difference in data.The Major difference is column order
In list tables schema column will be after table/view column
In list tables show details schema column will be after column type
IN db2warehouse I found that "owner" doesn't exist, so I describe table syscat.systables and try using CREATOR instead and it works.
db2 "select NAME from sysibm.systables where CREATOR = '[SCHEMANAME]'and type = 'T'"

How does one cheaply validate the existance of a column in a table in another schema with Oracle?

The environment is Oracle 9 & 10. I do not have DBA level access.
The problem is to verify that a specific column exists in a specific table, in another schema.
There are two cases to deal with.
Another schema in the same instance
A schema in a different instance, using a db_link
Given my schema FRED and another schema BARNEY, I tried something like this
SELECT 1
FROM BARNEY.USER_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
Which yielded [1]: (Error): ORA-00942: table or view does not exist
After vegging on this awhile, I realized that USER_TAB_COLS, is not really a table. It is a view. I have been selecting from tables all along, but not from a view.
I tried the same thing with my db_link, and was surprised to see data come back. A db_link has an embedded schema_name/password in it, so it seems reasonable to me that it worked, as it effectively logs in to the other schema, which should make the views reachable.
Having Googled around, and worn out my eyeballs on on the mountain of Oracle doc,
I am looking for someone to point me in the correct direction, or at least point out what I am missing.
What techniques are available for getting user table related metadata from a schema in the same instance in order to validate that a specific column exists?
Thanks in advance.
Evil.
+1 for good answers.
Thank you.
You can use the following query:
SELECT 1
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
AND OWNER = 'BARNEY';
(User_Tables and User_Tab_Cols are just views on all_tables and all_tab_coumns with a where owner = <Current User> attached to it)
If you're allowed to see the Barney's some_table (i.e. you have been GRANTed at least SELECT privileges on it), then you'll know if the column is there. If you have no rights on the table, you won't be able to get meta information on it.
As with the other replies, normally I use ALL_TAB_COLUMNS for a query like this. But that will only show columns in tables where you have SELECT. And it's select on that column -- in the unlikely event that they've implemented column-level privileges for that table, you may be able to see the table, but not see the specific column of interest. For most of us, that's extremely rare.
DBA_TAB_COLUMNS will show all columns, but you'll need select on it granted to your schema by your DBA. (Actually, you'll need a grant on ALL_TAB_COLUMNS to use it, but that's common in most shops). The DBMS_METADATA PL/SQL Built-in package can also be used, with similar limitations, but I think you'll find it more complicated.
Of course, you can also just try to select a record from barney.some_table.some_column#my_dblink (or whatever pieces of that you're interested in). And then handle the exception. Ugly, I wouldn't recommend it in most situations.
You would use all_tab_columns for that.
But beware that you'll only see what you are allowed to see.
Same instance, different schema:
Select Count(*)
From all_tab_cols
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
The count(*) has the advantage of always returning a single row with a value of either 1 or 0, so you do not have to deal with NO_DATA_FOUND errors in PL/SQL.
Across a DB Link, same schema as the one you connect as:
Select Count(*)
From user_tab_cols#MY_DB_LINK
Where table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
Across a DB Link, different schema than the one you connect as:
Select Count(*)
From all_tab_cols#MY_DB_LINK
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';

What is Mysql select statement to show a list of tables?

I want to write a program that can show the user a list of tables in the database and also show the descriptions of those tables. So can I do a "select * from system_table" or something like that?
This will give you a list of tables:
show tables;
To describe each table:
describe table_name;
To get both at the same time try:
SELECT * FROM DOMAIN.TABLES WHERE TYPE = 'TABLE'
SELECT * FROM DOMAIN.COLUMNS WHERE TABLETYPE = 'TABLE'
The results are similar to MySql show and describe statements
In addition to show tables, MySQL 5.0+ also supports the INFORMATION_SCHEMA meta-database:
SELECT table_name, table_comment FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';
information_schema.tables also has other information in it, if you're curious.
Note that if you didn't provide a comment when you created the table and use InnoDB, it will fill the table_comment column with unnecessary data, such as the InnoDB space reserved for this table or the foreign key constraints.

Get list of all tables in Oracle?

How do I query an Oracle database to display the names of all tables in it?
SELECT owner, table_name
FROM dba_tables
This is assuming that you have access to the DBA_TABLES data dictionary view. If you do not have those privileges but need them, you can request that the DBA explicitly grants you privileges on that table, or, that the DBA grants you the SELECT ANY DICTIONARY privilege or the SELECT_CATALOG_ROLE role (either of which would allow you to query any data dictionary table). Of course, you may want to exclude certain schemas like SYS and SYSTEM which have large numbers of Oracle tables that you probably don't care about.
Alternatively, if you do not have access to DBA_TABLES, you can see all the tables that your account has access to through the ALL_TABLES view:
SELECT owner, table_name
FROM all_tables
Although, that may be a subset of the tables available in the database (ALL_TABLES shows you the information for all the tables that your user has been granted access to).
If you are only concerned with the tables that you own, not those that you have access to, you could use USER_TABLES:
SELECT table_name
FROM user_tables
Since USER_TABLES only has information about the tables that you own, it does not have an OWNER column – the owner, by definition, is you.
Oracle also has a number of legacy data dictionary views-- TAB, DICT, TABS, and CAT for example-- that could be used. In general, I would not suggest using these legacy views unless you absolutely need to backport your scripts to Oracle 6. Oracle has not changed these views in a long time so they often have problems with newer types of objects. For example, the TAB and CAT views both show information about tables that are in the user's recycle bin while the [DBA|ALL|USER]_TABLES views all filter those out. CAT also shows information about materialized view logs with a TABLE_TYPE of "TABLE" which is unlikely to be what you really want. DICT combines tables and synonyms and doesn't tell you who owns the object.
Querying user_tables and dba_tables didn't work.
This one did:
select table_name from all_tables
Going one step further, there is another view called cols (all_tab_columns) which can be used to ascertain which tables contain a given column name.
For example:
SELECT table_name, column_name
FROM cols
WHERE table_name LIKE 'EST%'
AND column_name LIKE '%CALLREF%';
to find all tables having a name beginning with EST and columns containing CALLREF anywhere in their names.
This can help when working out what columns you want to join on, for example, depending on your table and column naming conventions.
For better viewing with sqlplus
If you're using sqlplus you may want to first set up a few parameters for nicer viewing if your columns are getting mangled (these variables should not persist after you exit your sqlplus session ):
set colsep '|'
set linesize 167
set pagesize 30
set pagesize 1000
Show All Tables
You can then use something like this to see all table names:
SELECT table_name, owner, tablespace_name FROM all_tables;
Show Tables You Own
As #Justin Cave mentions, you can use this to show only tables that you own:
SELECT table_name FROM user_tables;
Don't Forget about Views
Keep in mind that some "tables" may actually be "views" so you can also try running something like:
SELECT view_name FROM all_views;
The Results
This should yield something that looks fairly acceptable like:
Simple query to select the tables for the current user:
SELECT table_name FROM user_tables;
select object_name from user_objects where object_type='TABLE';
----------------OR------------------
select * from tab;
----------------OR------------------
select table_name from user_tables;
Try the below data dictionary views.
tabs
dba_tables
all_tables
user_tables
Execute the below commands:
Show all tables in the Oracle Database
sql> SELECT table_name FROM dba_tables;
Show tables owned by the current user
sql> SELECT table_name FROM user_tables;
Show tables that are accessible by the current user
sql> SELECT table_name FROM all_tables ORDER BY table_name;
Oracle database to display the names of all tables using below query
SELECT owner, table_name FROM dba_tables;
SELECT owner, table_name FROM all_tables;
SELECT table_name FROM user_tables;
vist more : http://www.plsqlinformation.com/2016/08/get-list-of-all-tables-in-oracle.html
Try selecting from user_tables which lists the tables owned by the current user.
With any of those, you can select:
SELECT DISTINCT OWNER, OBJECT_NAME
FROM DBA_OBJECTS
WHERE OBJECT_TYPE = 'TABLE' AND OWNER='SOME_SCHEMA_NAME';
SELECT DISTINCT OWNER, OBJECT_NAME
FROM ALL_OBJECTS
WHERE OBJECT_TYPE = 'TABLE' AND OWNER='SOME_SCHEMA_NAME';
select * from dba_tables
gives all the tables of all the users only if the user with which you logged in is having the sysdba privileges.
I did not find answer which would point to use
DBA_ALL_TABLES (ALL_ALL_TABLES/USER_ALL_TABLES)
so decided to add my version as well.
This view actually returns more that DBA_TABLES as it returns object tables as well (http://docs.oracle.com/cd/E11882_01/server.112/e40402/statviews_1003.htm).
A new feature available in SQLcl( which is a free command line interface for Oracle Database) is
Tables alias.
Here are few examples showing the usage and additional aspects of the feature. First, connect to a sql command line (sql.exe in windows) session. It is recommended to enter this sqlcl specific command before running any other commands or queries which display data.
SQL> set sqlformat ansiconsole -- resizes the columns to the width of the
-- data to save space
SQL> tables
TABLES
-----------
REGIONS
LOCATIONS
DEPARTMENTS
JOBS
EMPLOYEES
JOB_HISTORY
..
To know what the tables alias is referring to, you may simply use alias list <alias>
SQL> alias list tables
tables - tables <schema> - show tables from schema
--------------------------------------------------
select table_name "TABLES" from user_tables
You don't have to define this alias as it comes by default under SQLcl. If you want to list tables from a specific schema, using a new user-defined alias and passing schema name as a bind argument with only a set of columns being displayed, you may do so using
SQL> alias tables_schema = select owner, table_name, last_analyzed from all_tables where owner = :ownr;
Thereafter you may simply pass schema name as an argument
SQL> tables_schema HR
OWNER TABLE_NAME LAST_ANALYZED
HR DUMMY1 18-10-18
HR YOURTAB2 16-11-18
HR YOURTABLE 01-12-18
HR ID_TABLE 05-12-18
HR REGIONS 26-05-18
HR LOCATIONS 26-05-18
HR DEPARTMENTS 26-05-18
HR JOBS 26-05-18
HR EMPLOYEES 12-10-18
..
..
A more sophisticated pre-defined alias is known as Tables2, which displays several other columns.
SQL> tables2
Tables
======
TABLE_NAME NUM_ROWS BLOCKS UNFORMATTED_SIZE COMPRESSION INDEX_COUNT CONSTRAINT_COUNT PART_COUNT LAST_ANALYZED
AN_IP_TABLE 0 0 0 Disabled 0 0 0 > Month
PARTTABLE 0 0 0 1 0 1 > Month
TST2 0 0 0 Disabled 0 0 0 > Month
TST3 0 0 0 Disabled 0 0 0 > Month
MANAGE_EMPLYEE 0 0 0 Disabled 0 0 0 > Month
PRODUCT 0 0 0 Disabled 0 0 0 > Month
ALL_TAB_X78EHRYFK 0 0 0 Disabled 0 0 0 > Month
TBW 0 0 0 Disabled 0 0 0 > Month
DEPT 0 0 0 Disabled 0 0 0 > Month
To know what query it runs in the background, enter
alias list tables2
This will show you a slightly more complex query along with predefined column definitions commonly used in SQL*Plus.
Jeff Smith explains more about aliases here
You can use Oracle Data Dictionary to get information about oracle objects.
You can get list of tables in different ways:
select *
from dba_tables
or for example:
select *
from dba_objects
where object_type = 'TABLE'
Then you can get table columns using table name:
select *
from dba_tab_columns
Then you can get list of dependencies (triggers, views and etc.):
select *
from dba_dependencies
where referenced_type='TABLE' and referenced_name=:t_name
Then you can get text source of this objects:
select * from dba_source
And you can use USER or ALL views instead of DBA if you want.
We can get all tables including column details from below query:
SELECT * FROM user_tab_columns;
Including views:
SELECT owner, table_name as table_view
FROM dba_tables
UNION ALL
SELECT owner, view_name as table_view
FROM DBA_VIEWS
Below is a commented snippet of SQL queries describing how options you can make use of:
-- need to have select catalog role
SELECT * FROM dba_tables;
-- to see tables of your schema
SELECT * FROM user_tables;
-- tables inside your schema and tables of other schema which you possess select grants on
SELECT * FROM all_tables;
The following query only list the required data, whereas the other answers gave me the extra data which only confused me.
select table_name from user_tables;
I was looking to get a list of all columns names belonging to a table of a schema sorted by the order of column id.
Here's the query I am using: -
SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS
WHERE OWNER = 'schema_owner_username' AND TABLE_NAME='table_name'
ORDER BY COLUMN_ID ASC;
Indeed, it is possible to have the list of tables via SQL queries.it is possible to do that also via tools that allow the generation of data dictionaries, such as ERWIN, Toad Data Modeler or ERBuilder. With these tools, in addition to table names, you will have fields, their types, objects like(triggers, sequences, domain, views...)
Below steps to follow to generate your tables definition:
You have to reverse engineer your database
In Toad data modeler: Menu -> File -> reverse engineer -> reverse engineering wizard
In ERBuilder data modeler: Menu -> File -> reverse engineer
Your database will be displayed in the software as an Entity Relationship diagram.
Generate your data dictionary that will contain your Tables definition
In Toad data modeler: Menu -> Model -> Generate report -> Run
In ERBuilder data modeler: Menu -> Tool -> generate model documentation
To get all the table names, we can use:
Select owner, table_name from all_tables;
if you have dba permission, you can use:
Select owner, table_name from dba_tables;
select * from all_all_tables
this additional 'all' at the beginning gives extra 3 columns which are:
OBJECT_ID_TYPE
TABLE_TYPE_OWNER
TABLE_TYPE
Tables in the current user - logon schema
select * from tabs;