Find list of tables from list of cols [duplicate] - sql

This question already has answers here:
Find all tables containing column with specified name - MS SQL Server
(35 answers)
Closed 2 years ago.
I did google this a bit but my biggest problem is not knowing what to search. I felt like no matter what I searched it wasn't my problem so if you have a link to another question do to my search inability feel free to link it and be done with it.
Say I have like 250 different fields (columns) and they are all from 1 of 10 tables but I need to figure out which field comes from which table. Maybe i could do a massive select statement but there are possible duplicate column names and that sound like a headache
looking at each column name and the scouring through all these tables to find each one seems too difficult? Any thoughts? Does my question make sense? Let me know if you need more information.

you can use information_schema.columns to get information
select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME in ('col1', 'col2', ...)

For SQL Server you can find metadata information from the system base tables
SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
JOIN sys.columns AS c ON t.object_id = c.object_id
WHERE c.name IN('col1','col2') --give the columns names here.
Now, this code is specific to SQL Server and not a portable code.
But few cases to be noted that instead of using INFORMATION_SCHEMA which doesn't always have all the information like if the column has an IDENTITY property as it is proprietary to SQL Server.
You can read more about this here

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.

How to check, find and see the values of a specific column inside of a big database with many tables, if the column actually exists [duplicate]

This question already has answers here:
Find a string by searching all tables in SQL Server
(12 answers)
Closed 1 year ago.
I have a lot of tables inside some database of SQL Server and I think that some of those tables have some specific column that, I want to check if it exists and also see the values inside, if it actually exists.
I guess that the column was named like: ‘’Status’’
Please, consider that I don't have any idea about what are the values that maybe exist inside of this supposed column or even the kind of it.
Database name: PrincipalGroup
I won't say the name of the tables, because I don’t think it's feasible to write all the tables in this query, because there are many.
So, the point is: how can I query this by the easiest and simplest way?
You can try the following query that will produce a list of SQL statements you can then execute to "see the values inside" each table that contains the column named Status, and obviously tweak to your specific requirements.
select Concat('select distinct ', QUOTENAME(table_name, ''''), ' , [Status] from ', QuoteName(table_name))
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'Status'

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!

SQL search entire db for a value [duplicate]

This question already has answers here:
Find a value anywhere in a database
(18 answers)
Search all tables, all columns for a specific value SQL Server [duplicate]
(4 answers)
Closed 8 years ago.
I have "read" access to a database back end however the tables and columns are named oddly and I am unable to find the information I am looking for. (there are also a lot of tables and lots of data)
I have been using the following method:
Run query:
SELECT *
FROM information_schema.columns
WHERE TABLE_SCHEMA = 'dbname'
AND DATA_TYPE = 'varchar'
AND TABLE_NAME IN (SELECT TABLE_NAME
FROM information_schema.tables
WHERE TABLE_TYPE = 'BASE TABLE')
This gives me a list of columns which contain varchar values...
Then for each result, I run the following:
select top 1 [column name]
from [tablename]
where [column name] like 'value I'm searching for'
Is there a better way to do this? Or a way to combine these two queries together (as running the query on each result is a slow method)?
Thanks in advance

SQL Server - Constructing a Column Definition [duplicate]

This question already has answers here:
In SQL Server, how do I generate a CREATE TABLE statement for a given table?
(16 answers)
Closed 8 years ago.
I need to compare two databases and identify what columns have changed. Once I have identified a column that has changed in some way (size, type, etc) I need to capture (write to a table) the old column definition and the new column definition.
For instance, if using the INFORMATION_SCHEMA.COLUMNS table I discover that a column size has changed from 25 to 50 I will need to store the two column definition. In this case it may be 'char(25)' and 'char(50)'.
I have not problem using the INFORMATION_SCHEMA.COLUMNS table to identify when something has changed.
The issue I have is once I determine that the column has changed how do I build the column definition? In this case how to I build 'char(25)' and 'char(50)'?
Is there somewhere I can obtain this type of definition? If I have to build the definition piece by piece how do I determine all the components of the definition.
Any advice or suggestions are appreciated.
Thanks in advance.
select table_name, column_name,
column_definition = data_type + isnull('(' + convert(varchar, character_maximum_length) + ')', '')
from information_schema.columns