How to search a column in multiple tables in SQL Server 2005 - sql

There are several tables with many columns and it takes forever to manually find the column I need. How do I search for my column from whatever table it exists in?

You can do:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%columnName%'

You can use the sys.columns view and then join to the sys.tables view to get that information.

Related

Can I find 2 columns in the database- that exist in the same table?

In SQL Server, I have found many examples here how to find which tables contain a particular column.
SELECT COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%cash_log_id%'
ORDER BY TableName
,ColumnName;
I would like to know if there is a way to know which 2 columns are in the same table.
In my case, i am trying to find a column 'check number' but can't tie to one table that I can use.
So that in the above can we use column_name is one of.

SQL Server: how to view the insides of a column when looping through the whole database

I'm using
SELECT DISTINCT
COLUMN_NAME AS 'ColumnName',
TABLE_NAME AS 'TableName'
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME LIKE '%phone%'
ORDER BY
TableName, ColumnName;
Now, what I would like to know. How can I view what's written inside that column?
Because I'm searching for names, numbers, emails, etc. Since you may already know that this piece of code only displays the column and table name.
Thank you in advance, and if you need any more information, just ask away!

Postgresql how do i find a table based on column name?

I know that I have a table with the column "fortyid" but I cant remember which table it is and I have like 350 tables in my database.
Is there a way to find all tables that has "fortyid" as column? (doesn't matter the type)
You can use the metadata defined by the SQL standard, specifically INFORMATION_SCHEMA.COLUMNS.
select c.*
from information_schema.columns c
where c.column_name = 'fortyid';

How to get few column names among all columns in the one table with query in sql server 2008

select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='Submenu';
The above query gave all column names in Submenu table but I want only three column names of Submenu table.
Is there any way to get column names?
I assumed this is SQL Server, so the below queries might not work on other RDBMS's).
If your question is how to find only the column names you need (presuming you know in advance which ones those are), you would have to do something like this:
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
AND COLUMN_NAME IN ('Column1', 'Column2', 'Column3')
At this moment, you basically are requesting a list of any column within your table, without any restrictions whatsoever.
Alternatively, if you're looking only for the first three column names, this would work:
SELECT TOP 3
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
ORDER BY
ORDERINAL_POSITION
In the latter case, you will have to determine how you want to sort the column names though (either by using something like ORDER BY COLUMN_NAME, in case you want them listed alphabetically, or ORDER BY ORDERINAL_POSITION in case you're trying to get them in the order they appear in the table).
If this is not what you meant, please elaborate on what you are trying to achieve.
SELECT TOP 3 COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Submenu';
As easy as that!

Search Column name in Linked Server

Is there any way to find particular column in Linked Server's database within all tables.
I guess solution lies in
EXEC sp_columns_ex
SELECT t.name as TableName, c.name as ColumnName
FROM servernamehere.databasenamehere.sys.columns c
INNER JOIN servernamehere.databasenamehere.sys.tables t ON c.object_id = t.object_id
WHERE c.name like '%yoursearchhere%'
How about this:
EXECUTE [MyLinkedServer].[MyLinkedDB].dbo.sp_executesql
N'SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE ...'
And fill in the where clause depending on what you want to search for?
An alternative would be creating a view that selects from INFORMATION_SCHEMA.COLUMNS and then you query that instead.
Reference for Information_Schema.Columns
It would depend on which database your linked server is pointing to. For example, if it is Oracle, you would use Oracle syntax, if it is SQL Server, Sql Server Syntax.
The fact that you are querying the schema through a Linked server shouldnt matter.