Check if QTemp table exists in Db2 - sql

Is there a Db2 function that allows me to check if a table already exists?
I have a stored procedure that executes a command to create a table, however I only want to call it if the table doesn't exist.
I can't query the catalogs because files in Qtemp don't show in there.
Thanks,

You can query SYSIBM.SYSTABLES to have information about tables.
For your case:
SELECT count(1)
FROM SYSIBM.SYSTABLES
WHERE NAME = 'tableName' AND TYPE = 'T'
You can also check views and temprary tables by changing TYPE.
SYSIBM.SYSTABLES docs

Related

SQL Error: ORA-00955

I cant create this table.
It says that the columns names are already used by another object ???
ORA-00955
The error means that you have an object in your database with the same name that of your table.
Either use a different name for your new table
Drop the existing object with same name if you do not need it.
To check the existing object, you could query [ALL|USER|DBA]_OBJECTS view:
SELECT object_name, object_type FROM USER_OBJECTS WHERE OBJECT_NAME = 'DEPT_X1';;
The problem is not with your column name, but with the name of your table. There is already a table named DEPT_X1 in your database, perhaps because you've executed this command more than once. To verify this execute the following query:
SELECT * FROM USER_OBJECTS WHERE OBJECT_NAME = 'DEPT_X1';
If this query returns data it means that there is already something named DEPT_X1 in your database. To find out what it is look at the OBJECT_TYPE column in the data returned by the query above. If OBJECT_TYPE = 'TABLE' it means that the existing DEPT_X1 is a table; if OBJECT_TYPE = 'SEQUENCE' it means DEPT_X1 is a sequence; etc. Let's say for the sake of argument that the existing DEPT_X1 is a table and that you want to get rid of the one that's already there so you can create another. You'd want to use a DROP TABLE command similar to the following:
DROP TABLE DEPT_X1;
After running the above successfully you could then re-create the table using your existing script. If the existing DEPT_X1 is some other kind of database object you'd have to use the correct DROP command for that particular kind of object - there are many different DROP commands which are documented here (see chapters 17 and 18).
Best of luck.

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

Sql Server 2005 Database table is not visible whereas stored procedure is visible

Sir,
I've a problem regarding Database.
My problem is I've Database script file.When I integtrated the script file in Database.I found all the stored procedure is visible.But tables are not visible.
I wrote the Command as follows:
select * from sysobjects where type='p'
Now all the tables is showing but fields are not.
I wrote the Command to get the fields for particular table_name targeted as
select * from table_name .
But a message is Coming like Invalid Object name as table_name.
Please help me out to find the fields of the table ...
You can use the following query to retrieve the columns of the sought-for table:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your table name'
You can list all columns with:
select * from sys.columns

In sqlite How to add column in table if same column is not exists in table

How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'

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'"