How to search informix database for a column - sql

I want to be able to search an Informix DB for a column. In the main table there is a cust_nbr column, that is then referenced in a unknown number of tables.
In Informix, is there a way to query the DB and get all the tables that use the cust_nbr?

SELECT tabname, colno, colname
FROM systables a, syscolumns b
WHERE a.tabid = b.tabid
and colname = "cust_nbr"
ORDER BY colno;
I found this code in the same place and added the extra restriant with colname = cust_nbr.
This seems to have worked for me. i will verify it, but all signs look like it worked.
i found that in the Using the Informix Catalogs mentioned in the other post

You should be able to get this type of thing from the system catalog tables, sysreferences in particular. As taken from Using the Informix System Catalog:
SELECT a.tabname, constrname, d.tabname
FROM systables a, sysconstraints b, sysreferences c,
systables d
WHERE b.constrtype = 'R'
AND a.tabid = b.tabid
AND b.constrid = c.constrid
AND c.ptabid = d.tabid
AND a.tabname = ?;

Related

How to get column names from a query in SQL Server

Using SQL Server.
I have a very extensive query, with a lot of aliasing, etc...
Is there a way, using just SQL (stored proc is fine, but not PHP, etc), to get a list of all column names from this query? (I realize I will have to probably embed my query inside of this solution but that is fine. Just a temporary measure.)
Thanks!
If you're using SQL Server 2012 or later you can take advantage of sys.dm_exec_describe_first_result_set
SELECT name
FROM
sys.dm_exec_describe_first_result_set
('Your Query Here', NULL, 0) ;
DEMO
There are various ways that you can get the columns out of the query, such as:
select top 0 s.*
from (<your query here>) s;
Then you can parse the results.
However, I have found another approach useful. Create either a view or a table using the same logic:
select top 0 s.*
into _TempTableForColumns
from (<your query here>) s;
Then use information_schema (or the system tables if you prefer):
select *
from information_schema.columns
where table_name = '_TempTableForColumns' and schema_name = 'dbo';
drop table _TempTableForColumns;
The advantage of this approach is that you can get type information along with the column names. But the engine still has to run the query and that might take time even though no rows are returned. Although the column names and types are available after compiling, I am not aware of a way to get them without also executing the query.
After SQL Server 2008
select *
from sys.columns c
inner join sys.objects o on c.object_id = o.object_id
where o.name = 'TableName'
Before
select *
from syscolumns c
inner join sysobjects o on c.id = o.id
where o.name = 'TableName'

last time a table was accessed in oracle

Is it possible to determine when the last time a table was accessed in Oracle?
I am trying this, but this will not include when was the last time the table was selected.
select * from dba_objects;
select p.object_owner owners, p.object_name Obj_Name, p.operation Operation,
p.options Options, count(1) Idx_Usg_Cnt
from dba_hist_sql_plan p,dba_hist_sqlstat s
where p.object_owner = '&USERNAME' and p.operation like 'TABLE%'
and p.sql_id = s.sql_id and p.object_name=’&OBJNAME’
group by p.object_owner,p.object_name,p.operation,p.options order by 1,2,3
From #KD 's answer I've developed this query to detect unused tables:
select a.obj#, b.object_name, b.owner, b.object_type, b.timestamp
from dba_hist_seg_stat a, dba_objects b
where a.obj# = b.object_id
and b.owner = 'YOUR_SCHEMA_NAME'
and object_name = 'A_TABLE_NAME'
order by timestamp desc
Since this query makes use of SYS user views, you must have special privileges to run it. If you have them, you can have a look at dba_hist_XXX views, or only have a look at the rest of the columns in the views used here: you have info about reads, writes, locks and many more.
EDIT: Thanks to #APC for the warning. DBA_HIST_XXX are views from Diagnostics Pack that require special license.
I'm using the SQL below to find the list of segments getting full table scaned and how many times
they have been full table scanned..is this SQL is correct with respect to this.
select a.obj#,a.table_scans_delta,b.object_name,b.owner,b.object_type
from dba_hist_seg_stat a, dba_objects b
where a.obj# = b.object_id
and b.owner like 'USERNAMES%'
order by table_scans_total desc

Finding Indexes For A Table in Sybase

I am currently working in an environment where I am made double blind.
I have to email any queries I need to run to a employee of the client
That employee runs them in a SAS client, with an odbc connection to SyBase
I need to find out how to ascertain exactly what indexes exist on a specific table. I would use sp_helpindex, but apparently it doesn't exist on their instance of SyBase.
We believe that it is SyBase 12, but again I can't be certain of anything.
Does anyone have SQL for...
- Confirming exactly what version of SyBase we're working on?
- The details of all indexes that exist for a given table?
Most Sybase products have a -v command line argument to tell the version with or without the engine running.
To find the indexes on any table enter the following, where my_table is your table's name:
select i.name
from sysindexes i, sysobjects o
where o.name = 'my_table'
and o.id = i.id
To "Confirming exactly what version of SyBase we're working on?"
Why not use:
select ##version
Sybase website is down (at least here), but it would be something like:
IQ SHOW INDEXSET INDEXES
or
IQ SHOW INDEXSET FOR indexset
where indexset is tablename because every table has a indexset assigned with the same name.
If you have access to sybase website possibly you can go further :)
You can use this query:
select i.name
from sysindexes i, sysobjects o
where o.name = 'table_name' and i.indid >=1
and o.id = i.id
Find Indexes on multiple tables, not like image indexes
select name = o.name,iname = i.name from sysindexes i, sysobjects o where o.name in ('table1','table2','table3') and i.name not like 't%' and i.indid >=1 and o.id = i.id
For sybase version:
select ##version
in Sybase version SAP IQ/16, you can get list of indexes with following (table name my_table is case sensitive:
select *
from sys.sysindexes
where tname = 'my_table';
select * from sys.sysindexes where tname='Your Table name'
Try the above code, it worked for me.

Retrieving column and other metadata information in Teradata

I have a half dozen views in SQL Server that I need to replicate in Teradata, but I haven't been able to find the TD equivalent of the SQL metadata tables. I'd like to replicate the following functionality (which I assume is fairly self-explainatory):
select table_name, column_id ordinal_position, column_name,
data_type, char_length char_max_length,
data_precision numeric_precision, data_scale numeric_scale
from user_tab_columns
select name as FUNCTION_NAME
from sys.objects
where type_desc='SQL_SCALAR_FUNCTION'
select TABLE_NAME as VIEW_NAME
from INFORMATION_SCHEMA.VIEWS
I'd also like to know if there are any usable Teradata references online; everything I run across seems to be advertising rather than practical information.
All Teradata system tables are stored under DBC schema.
For columns, it is dbc.columns
select * from dbc.columns
For views, it is dbc.tables with a filter on a column something named table_type 'V' (where V stands for Views)
select * from dbc.tables
I am not sure about how to get all functions in Teradata. Whoever knows it, please edit this answer.
In Teradata DBC.Tables contains many of the objects that exist on the system. (e.g. Stored Procedures, UDF, Triggers, Macros, Views, Tables, Hash Index, Join Index, etc.) The column Table Kind is used to identify the type of object.
SELECT *
FROM DBC.TABLES
WHERE TABLEKIND = '<see below>'
A = Aggregate Function
B = Combined Aggregate Function and ordered analytical function
D = JAR
E = External Stored Procedure
F = Standard Function
G = Trigger
H = Instance or Constructor Method
I = Join Index
J = Journal
M = Macro
N = Hash Index
O = No Primary Index (Table)
P = Stored Procedure
Q = Queue Table
R = Table Function
S = Ordered Analytical Function
T = Table
U = User-defined data type
V = View
X = Authorization
Y = GLOP Set

Accessing Database Meta Data

Im running into a tough issue. I have a database using Microsoft SQL 2008 and in this database there are many tables. The tables were auto generated and do not have meaningful names. There is one particular table that I need, and I can not seem to find it.
I know what the names of a few of the columns in the table are called. Is there a way I can go through all the tables one at a time looking at the names of the columns and seeing if they match the ones I know.
If they do, then I can look farther into it the table to see if it is the one I am looking for. Does this sound like a good approach to the problem? Is it possible? Any ideas of where to start?
SELECT OBJECT_SCHEMA_NAME([object_id]),
OBJECT_NAME([object_id])
FROM sys.columns
WHERE name IN ('column 1', 'column 2'
/* , ... other columns */);
EDIT by request, in case the OP meant to identify ALL vs. ANY:
SELECT OBJECT_SCHEMA_NAME([object_id), name
FROM sys.tables AS t
WHERE EXISTS
(
SELECT 1 FROM sys.columns
WHERE name = 'column 1'
AND [object_id] = t.[object_id]
)
AND EXISTS
(
SELECT 1 FROM sys.columns
WHERE name = 'column 2'
AND [object_id] = t.[object_id]
)
/* ... repeat for other columns ... */
Alternative to Aaron's answer using Information_schema.columns instead of sys.columns
SELECT Table_name
FROM
information_schema.columns
WHERE
column_name IN ('column 1', 'column 2')
GROUP BY Table_Name
Having COUNT(column_name) = 2
See this Data.SE query for a working example
With the scripts above, you are limited to SQL wild-carding, which can be pretty limited. You can use SchemaCrawler grep to more powerfully search through your database using regular expressions. SchemaCrawler also allows you additional features to to look for tables related by foreign keys, so for example, you can say find me all tables that have a customer address column, along with the tables that refer to these tables. SchemaCrawler is a command-line tool that is bundled with a Microsoft SQL Server database driver.
Sualeh Fatehi, SchemaCrawler