SQL Query to lookup a column - sql

I want to lookup if a column by the name of 'LOB' exist among thousands of views.
Schema name: PACONE
any help is appreciated here. Thanks.

That solution will depend on what Database Server you have.
If you have Oracle, go to Search an Oracle database for tables with specific column names?
If you have mySQL, go to How do i search a mysql database for a specific column name
If you have PostgreSQL, go to How to find a table having a specific column in postgresql
If you have SQL Server, this is a possible solution:
How-To: Find Fields or Tables Within a SQL Server Database
Suppose you need to find a field called GLASS_ID, you simply run this:
-- SEARCHING FOR A CERTAIN FIELD NAME --
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE UPPER(COLUMN_NAME) = 'GLASS_ID'
Similarly, you can look for all field names that end with 'CO'
-- SEARCHING FOR FIELD NAMES THAT END WITH... --
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE UPPER(COLUMN_NAME) LIKE '%CO'
Of course, this is supposed to work under SQL Server, and that was tested in SQL Server 2005 only, but I guess that it will work in any recent version too.

use [your database name]
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name = 'LOB'
ORDER BY schema_name, table_name;

First of all you should provide some information about your environment.
If you use Oracle you can query the dba_tab_columns with dab_view (user_ or all_ is also possible) e.g.:
select a.view_name
from dba_views a
join dba_tab_columns b
on a.view_name = b.table_name
where lower(b.column_name) = 'lob'
and lower(b.owner) = <view_owner>;

Related

SQL Information_Schema Syntax for finding servername, table and column details

I am looking for SQL Information_Schema syntax to retrieve Server Name, Table Name, Column Name, View and all the details.
I also need to find which Table : Column has Image files.
Is there any specific syntax to help me out.
There are many different information_schema views. If you want to see what views are available then in SSMS object explorer you can navigate to databases > system databases > msdb > views > system views and scroll down to the information_schema. Since these are views you can just query them. From your question the ones you'll be interested in are
INFORMATION_SCHEMA.columns
INFORMATION_SCHEMA.tables
INFORMATION_SCHEMA.views
Here's an example query that lists servername and all columns
select ##servername, *
from INFORMATION_SCHEMA.columns
You can join the views and filter your data just like you would any query. Hope this helps.
select *
from information_schema.tables t
join INFORMATION_SCHEMA.columns c
on t.table_name = c.table_name
where c.data_type = 'image'

How to update column name after finding the table names from information schema

I have run a script to find the table names with a specific column name from a database using the information schema
here is the query
use IMS_SCMS_DIGITAL_POWER
select * from information_schema.columns
where column_name like 'COMPANY_ID%'
after finding the table names now i would like to update the specific column values of all the database. Need solutions.
Run following query to get all tablenames with specific column name
SELECT t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%COMPANY_ID%'
just use in forward only cursor with tablenames to update the specific table.

Dynamically determining table name given field name in SQL server

Strange situation: I am trying to remove some hard coding from my code. There is a situation where I have a field, lets say "CityID", and using this information, I want to find out which table contains a primary key called CityID.
Logically, you'd say that it's probably a table called "City" but it's not... that table is called "Cities". There are some other inconsistencies in database naming hence I can never be sure if removing the string "ID" and finding out the plural will be sufficient.
Note: Once I figure out that CityID refers to a table called Cities, I will perform a join to replace CityID with city name on the fly. I will appreciate if someonw can also tell me how to find out the first varchar field in a table given its name.
SELECT name FROM sysobjects
WHERE id IN ( SELECT id FROM syscolumns WHERE name = 'THE_COLUMN_NAME' )
To get column information from the specified table:
SELECT column_name, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'myTable'
select table_name from information_schema.columns where column_name='CityID'
You can use the INFORMATION_SCHEMA tables to read metadata about the database.
SELECT
TABLE_NAME
FROM
[db].[INFORMATION_SCHEMA].[COLUMNS]
WHERE
COLUMN_NAME='CityID';
For a primer in what's in the INFORMAITON_SCHEMA, see INFORMATION_SCHEMA, a map to your database
The information you seek is all available in the information schema views. Note that you will find many sources telling you how to directly query the underlying system tables that these are views onto - and I must admit that I do the same when it's just to find something out quickly - but the recommended way for applications is to go through these views.
For example, to find your CityID column:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'CityID'
To find the first varchar field in a table:
SELECT TOP 1 * FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME = 'TableName'
AND DATA_TYPE = 'varchar' -- This is off the top of my head!
ORDER BY ORDINAL_POSITION
As I understand from your question, you want to find tables which contain CITYID column in primary key
You can use SQL Server system views like sysindexes and sysindexkeys as shown in SQL tutorial to query database table primary keys including composite primary keys which are formed
SELECT
TBL.name as TableName
FROM sysobjects as PK
INNER JOIN sys.objects as TBL
on TBL.object_id = PK.parent_obj
INNER JOIN sysindexes as IND
on IND.name = PK.name AND
IND.id = TBL.object_id
INNER JOIN SysIndexKeys as KEYS
on KEYS.id = IND.id AND
KEYS.indid = IND.indid
INNER JOIN syscolumns as COL
on COL.id = KEYS.id AND
COL.colid = KEYS.colid
WHERE
PK.xtype = 'PK' AND
COL.name = 'CityID'

Given a column name how can I find which tables in database contain that column?

Given a column name how can I find
which tables in database contain that
column ?
or alternatively
How can I find that particular column
exists for all tables in Database ?
Note: Kindly explain answers with Examples as that I get most knowledge from the answer.
Edit: I am using MySQL Database.
SELECT * FROM information_schema.columns WHERE COLUMN_NAME = 'mycolumn'
Depends on the database you are using. Many database systems expose a set of tables of views that contain details of the schema. For example, you can get schema information from the SYSTABLE and SYSCOLUMN views in Sybase ASA.
in SQL Server:
select distinct t.name
from sys.Columns c
inner join sys.tables t on c.object_id = t.object_id
where c.name = 'YOUR_COLUMNNAME'

Finding all Nullable Columns in SQL 2000 Database

How to find out column with NULL values allowed in the insert in whole database ?
I don't have sql at hand, but the query goes something like this
SELECT * FROM information_schema.columns WHERE is_nullable = 'YES'
In general, search for this stardard view, for all the metadata info about your schema and structure of the database; there are many others (information_schema.tables, information_schema.constraints, etc)
Those who only want to see columns from base tables (not views) should join with INFORMATION_SCHEMA.TABLES. I also like to exclude the system table sysdiagrams.
Query
SELECT
c.TABLE_NAME,
COLUMN_NAME,
DATA_TYPE
FROM
INFORMATION_SCHEMA.COLUMNS AS c
JOIN INFORMATION_SCHEMA.TABLES AS t ON t.TABLE_NAME = c.TABLE_NAME
WHERE
is_nullable = 'YES' AND
TABLE_TYPE = 'BASE TABLE' AND
c.TABLE_NAME != 'sysdiagrams'
ORDER BY
c.TABLE_NAME,
COLUMN_NAME
If you have duplicate table names across schemas or table catalogs, you should involve those fields in the join as well, as shown in the answers here:
Differentiating tables and views in INFORMATION_SCHEMA.COLUMNS.