SQL : How to search column name in DB (within set of tables) - sql

If my database has large set of tables then -
Is there any way / query which will search - which table contains particular column name ?
Eg. I want to know the name of table which contains column par_token.
How can I achieve this? - I am using SQL Management Studio 2014.

You can use the sys objects:
SELECT s.[name] AS SchemaName,
t.[name] AS TableName
FROM sys.schemas s
JOIN sys.tables t ON s.schema_id = t.schema_id
JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.[name] = N'par_token';

I recommend using INFORMATION_SCHEMA.COLUMNS. The INFORMATION_SCHEMA views are (relatively) standard views available across databases. And, you don't need any joins:
select c.*
from information_schema.columns c
where c.column_name = 'par_token';

Related

How to show column names when you in different catalog in SQL Server?

Suppose there are 2 catalogs. First is 'master', second is 'test'. I want to see columns of test catalog's tables when my current catalog is master.
There is command is like:
SELECT
sys.columns.name AS ColumnName
FROM
sys.columns
JOIN
sys.tables ON sys.columns.object_id = tables.object_id
This command getting current schemas columns. It means I have to change my catalog master to test. Is there any way to show columns without USE test command?
You could use 3-part name:
SELECT c.name AS ColumnName
FROM master.sys.columns c
JOIN master.sys.tables t
ON c.object_id = t.object_id
UNION ALL
SELECT c.name AS ColumnName
FROM test.sys.columns c
JOIN test.sys.tables t
ON c.object_id = t.object_id

Find all Tables that contain Column Name; Filter Empties

I've been using the SQL found here:
Find all tables containing column with specified name
to great success. It allows me to find all tables that contain a certain column. My issue is that the database I'm working on seems to have a lot of empty tables (maybe around half of my results are empties). I was wondering if there was a way to modify the code in the link such that empty rows/columns are not presented.Below is the code from the link:
SELECT c.name AS 'ColumnName'
,t.name AS 'TableName'
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%MyName%'
ORDER BY TableName
,ColumnName;
Thank you,
Something like this may work without huge effort:
SELECT c.name AS 'ColumnName'
,t.name AS 'TableName'
,p.rows
FROM sys.columns c
INNER JOIN sys.tables t
ON c.object_id = t.object_id
INNER JOIN sys.partitions p
on t.object_id = p.object_id
WHERE c.name LIKE '%p%'
AND p.rows > 0
ORDER BY TableName
,ColumnName;
Just note that sys.partitions isn't guaranteed to be accurate about row counts; sys.dm_db_index_physical_stats may be better (see https://dba.stackexchange.com/questions/55124/how-accurate-is-the-sys-partition-rows-column). This might give you a better count but may have more locking issues if you're using AlwaysOn:
SELECT c.name AS 'ColumnName'
,t.name AS 'TableName'
,ips.record_count
FROM sys.columns c
INNER JOIN sys.tables t
ON c.object_id = t.object_id
CROSS APPLY sys.dm_db_index_physical_stats(DB_ID(), t.object_id, null, null, 'DETAILED') ips
WHERE c.name LIKE '%p%'
AND ips.record_count > 0
ORDER BY TableName
,ColumnName;
If you really need 100% reliability on this, you'd have actually do a COUNT(*) on each table (using Dynamic SQL) you want to check, but this is probably good enough.

What is the easiest way to search for user-defined columns in Intersystems Cache using SQL?

I am trying to search an extremely large Cache DB for columns that match a particular text string. In T-SQL, we could simply query the sys.columns view and then join it to sys.tables to get the name of all tables that have a column that matches the text we're looking for.
SELECT b.name AS table_name, a.name AS column_name
FROM sys.columns a
INNER JOIN sys.tables b ON a.object_id = b.object_id
WHERE b.type = 'U' AND a.name LIKE '%SEARCHTEXT%'
I've tried querying the %DICTIONARY.COMPILEDPROPERTY, but I haven't figured out how to limit the result set to user-defined classes. Is there a particular property that will limit the result set as desired? Perhaps a pattern on the class name?
This query:
SELECT
parent AS class,
parent->SqlQualifiedNameQ As table_name,
name AS property
FROM %Dictionary.CompiledProperty
WHERE NOT parent->id %STARTSWITH '%'
Executed in every namespace beyond %SYS/SAMPLES/DOCBOOK would return user defined tables (system tables start with %, users should not use table names starting with %)
What version of Caché do you have?
Starting with 2015.1 you can use INFORMATION_SCHEMA schema
SELECT b.table_name AS table_name, a.column_name AS column_name
FROM INFORMATION_SCHEMA.columns a
INNER JOIN INFORMATION_SCHEMA.tables b ON a.table_name = b.table_name
WHERE b.table_type <> 'SYSTEM TABLE' and a.column_name LIKE '%Age%'
Full list of tables in INFORMATION_SCHEMA is in Class Reference http://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?PAGE=CLASS&LIBRARY=%25SYS&PACKAGE=1&CLASSNAME=INFORMATION.SCHEMA

How to check if a column has not null constraint?

I am using SQL Server 2008 R2.
I have a table in which I have a column that have a not null constraint.
Now, what if I want to check if column has not null constraint defined or not for specific column?
Is there any query to find out it?
Thanks in advance..
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
This query will show all columns from all tables and a whole host of information on them. The column you would want is: IS_NULLABLE that can have the value 'YES' or 'NO'
COLUMNS (Transact-SQL)
Something like
SELECT o.name AS tab, c.name AS col, c.is_nullable
FROM sys.objects o
INNER JOIN sys.columns c ON c.object_id = o.object_id
WHERE o.name like '%yourtable%' and type = 'U'
See sys.columns and sys.objects
There's some catalog views you can use:
// information about check constraints
select * from sys.check_constraints
// information about specific columns
select name, is_nullable from sys.columns
// information about tables
select * from sys.tables
The sys.columns is_nullablefield holds information about nullability.
there is a table sys.all_columns
and a column in this table called is_nullable
http://technet.microsoft.com/en-us/library/ms177522(v=sql.105).aspx
select s.name, c.name, c.is_nullable from sys.tables s, sys.all_columns c
where s.object_id = c.object_id
and s.type = 'U' -- USER_TABLE
and c.is_nullable = 1
It is a simple command that will list - Field, Type, Null, Key, Default
SHOW FIELDS FROM Your_Table_Name;

How to get column metadata from a table synonym

How can I determine column metadata from a table synonym in a SQL Server 2005 database? I have a synonym called 'ProjectSyn' for a table called 'Project', but I can find no column metadata for the synonym.
My guess is to somewhere determine the 'base table' for the synonym, then query for column metadata for that table. Is this a correct approach, and if not, what would be?
This is my solution which works with synonyms of different databases:
SELECT TOP 0 * INTO #TEMP1 FROM YourTable
SELECT
[column_name] = c.name,
[data_type] = t.name,
[character_maximum_length] = c.max_length
FROM tempdb.sys.columns c
inner join tempdb.sys.types t on t.system_type_id = c.system_type_id
WHERE [object_id] = object_id('tempdb..#TEMP1');
DROP TABLE #TEMP1
Something like this? (edited)
select c.*
from
sys.columns c
inner join sys.synonyms s on c.object_id = object_id(s.base_object_name)
where
s.name = 'ProjectSyn'
Yes, I think getting the base object and then retrieve the columns, is your only option.
To get the base object name for a synonym, just query the view sys.synonyms