When running a simple catalog query:
SELECT *
FROM SYSCOLUMNS
I get back only a few hundred rows. This is DB2 7.1 on an AS400. There are dozens of schemas, more than a thousand tables in total. I expect this query to return several thousand rows, not a few hundred. The rows I do get back are only from my test schema and that of one other developer.
I am new to DB2 (20+ years in Oracle), so this is puzzling. The IBM doc says that SELECT is granted to PUBLIC on SYSCOLUMNS, so I should get everything, right? I don't know what the token "SYSCOLUMNS" is pointing to, a view, or local table (which would explain things). In my environment, every query to the database requires SCHEMA_NAME.TABLE_NAME, so this SYSCOLUMNS (with no schema name) is already an exception.
thanks in advance,
db
The answer will depend on what naming mode you're using.
*SQL mode: You qualify tables as SCHEMA.TABLE
*SYS mode: You qualify tables as SCHEMA/TABLE
(Behind the scenes, the schema maps to a library and the table maps to a file.)
In *SQL mode, if you don't specify a schema, the value of CURRENT SCHEMA is used. By default your CURRENT SCHEMA is your user profile, but you can change it like so:
SET CURRENT SCHEMA = SOMELIB
In *SYS mode, if you don't specify a schema, the library list is used to resolve the table name to a particular file.
Anyway, I'm going to guess you're in *SQL naming mode and your current schema is set to your test schema. Querying the un-qualified SYSCOLUMNS will give you the columns in your current schema.
I would try the following, which should widen the scope to all columns on the system:
SELECT * FROM QSYS2.SYSCOLUMNS
Related
I need to get complete list of external tables in db2 database. I have defined schema called DB2INST1. How to get complete list of external tables information using system tables?
The information lives in syscat.tables (documentation here) for Db2-LUW databases that support external tables, which have their PROPERTY column with value Y in position 27 of that column.
Example query returns the fully qualified name of external tables:
select trim(tabschema)||'.'||rtrim(tabname)
from syscat.tables
where substr(property,27,1)='Y'
with ur;
In general, the best and most reliable way to retrieve all information and to recreate the DDL statements for tables is to use the db2look tool. If you want to extract the metadata on your own, there are some catalog views to start with:
SYSCAT.TABLES holds the table information. Look for the PROPERTY column and check there if it is an external table.
SYSCAT.COLUMNS has the basic column information. But there are more related tables depending on types and attributes.
SYSCAT.EXTERNALTABLEOPTIONS shows the actual options for an external table, the things in addition to what makes a regular table.
There are many more tables to hold table properties, depending on the complexity of the table and column definitions.
What is the difference between these two queries:
select * from syscat.tabauth
select * from sysibm.systabauth where tcreator='SYSCAT' and ttname='TABAUTH'
Are they same ?
EDIT:
1. select grantee from sysibm.systabauth where tcreator='SYSCAT' and ttname='TABAUTH' and selectauth='Y'
select grantee from syscat.tabauth where selectauth='Y'
Would there be any difference in the value of these two queries ???
If I change selectauth to 'N' using sysibm.systabauth. Does that reflect in query 2?
The main difference is that one is a table and the other is a readonly view.
Other differences exist, and they can be version specific.
Different permissions can apply also.
When your target database are always on Linux/Unix/Windows , use SYSCAT schema as IBM tries to keep that unchanged even if the underlying objects change between versions (except where new columns get added ). IBM describes the SYSCAT schema here.
The SYSCAT schema contains many views and is relevant for Linux/Unix/Windows versions of Db2-Servers.
The SYSIBM schema contains many tables and is present on both Z/OS and LUW versions of Db2-servers.
So SYSCAT.TABAUTH is only a view on SYSIBM.SYSTABAUTH and you can see the definition of the view in the catalog with a query like this:
"select substr(text,1,4096) from syscat.views where viewschema='SYSCAT' and viewname='TABAUTH'"
You use GRANT and REVOKE statements to alter the contents of the SYSIBM.TABAUTH table directly, other statements like CREATE/DROP/ALTER table can indirectly change its contents.
I have an Access query, which references a table: dbo_table2
However, I have moved this table to another database, so I need to fully qualify to restore the links.
SELECT table1.x, table1.y, table2.z
FROM dbo_table1
INNER JOIN dbo_table2
ON (dbo_ID = dbo_ID)
Both tables are being pulled from SQL tables.
In my Access query I have added table2 as a Linked Table.
But I'm unsure of Access syntax. If I was using SQL, I would simply use "newdatabaseDB.dbo.table2".
My question is how can I correctly name the table2 reference by fully qualifying the database and table name.
If you moved your table to another db, just Link that table in your current db. This way you can use just as it was local.
There is another option, using IN:
select * from clients in 'c:\test\mydb.mdb'
Also see https://stackoverflow.com/a/3123395/78522
Cracked it!
By creating a link to an external table in the database where the query was created, Access then treats the table in the queries as if it were local (so no fully qualification needed).
I am using Orable DB in my application and SQL Developer Query Browser for UI Purpose.
Today I faced a very strage issue, I run a query in the query browser which gives me records successfully. But under the connection -> Table tree hierarchy, no tables are displayed.
From stackoverflow I got a solution from here : SQLDeveloper displays no tables under connections where it says tables
But under "Other Users" tree, it displays large list of schemas. So I am confused which schema is used by me query.
Any suggestion ??
Assuming that you are running a query that involves an unqualified identifier
SELECT *
FROM some_object
and that you haven't done something odd like changing the current_schema of the session, the object is either something that exists in your schema or is a public synonym.
SELECT owner, object_type, object_name
FROM all_objects
WHERE object_name = 'SOME_OBJECT'
will show you all the objects that you have access to with that name.
What I am trying to do is verify a URL. I just need to be able to select that single value from all databases that we have currently in SQL Server 2008. All the databases are the same, just multiple instances of the same database for different users.
I am looking to pull one item from one table in each database.
Each database contains a table SETTINGS and within that table a value for MapIconURL. I need that value from each table from within each database. I am looking at around 30 or so databases that would have this value.
So I found the "undocumented" Stored Proc sp_MsForEachDb and have working....to a point.
The code I am using is this:
EXEC sp_MsForEachDb 'use ?; SELECT "?" as databasename,SETTINGSKEYID, SECTION, NAME, INIVALUE, DESCRIPTION
FROM ?.dbo.SETTINGS
WHERE [NAME] = "MapIconURL"'
I have noticed that it is not selecting all the databases, but that it is also selecting the master table as well as other system tables, and am thinking that may be why it is not selecting all tables. Is there a way to exclude the system related tables?
If the number (and name) of the databases is fixed, then you could simply do:
SELECT MapIconUrl FROM db1.dbo.SETTINGS
UNION ALL
SELECT MapIconUrl FROM db2.dbo.SETTINGS
...
However, if either the number or names of the databases is not fixed, then you have to build the query dynamically.
First, run a query such as SELECT name FROM master..sysdatabases to get the names of the databases.
Then, loop over the result set (in T-SQL, use a CURSOR) and build your query and execute it (in T-SQL, use sp_executesql).