Need to find specific column name from database - sql

I have one database on my server I want to filter one column name from the whole database where that column is used.
E.g: If column name is "AlternativeID" is exist in 5 tables and than I want the query that find this column name exists in which tables.?
I find the following query to find specific column name from database.
SELECT * FROM sys.columns WHERE name LIKE '%AlternativeID%'
I hope this is make sense to everyone. Any help will appreciated. Thanks!!

Try this:
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%AlternativeID%'
OR...
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%AlternativeID%'
Source : Find all tables containing column with specified name
Hope this helps...

Try:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('AlternativeID')
AND TABLE_SCHEMA='YourDatabaseName';

Related

How can I filter tables from list of tables according to specific word in their name and then combine all the filtered tables into one table in sql?

I use this query to filter but I don't know how to combine all the filtered tables at once
SELECT table_name
FROM information_schema.tables
WHERE table_name LIKE '%_exp%'
then combine all the filtered tables into one table in sql?
You can use select into. In SQL Server, use sys.tables
SELECT schema_name(schema_id) SchemaName, [name] TableName
INTO <NewTable>
FROM sys.tables
WHERE schema_id = schema_id('dbo') and [name] LIKE '%_exp%';
/* specify the desired schema or remove to include all */

SQL query for finding number of columns

I want to find the number of columns in a table but I want to find them using the LIKE condition.
Is there anyway I can do that?
Thank you very much
Assuming you are using sql server, try the following query:
SELECT COUNT(*)
FROM SYS.COLUMNS c
INNER JOIN sys.tables t ON t.object_id = c.object_id
WHERE t.name like 'TABLE_NAME'
Fill in your like condition instead of "TABLE_NAME"

search for multiple columns in a single table sql

I am using SQL Server 2017. How can I search for multiple column names in a single table?
I can search for a single column name (USERID) in my database with:
select *
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%USERID%'
order by TABLE_NAME
But, I want to search for all tables that have both USERID and DOB.
Using:
select *
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%USERID%' OR COLUMN_NAME like '%DOB%'
order by TABLE_NAME
returns a list of all tables that contain the column USERID as well as all tables that contain DOB.
Changing to AND results in a completely empty list, which is correct, as a single column would not contain both terms
You can try below -
select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%USERID%' OR COLUMN_NAME like '%DOB%'
group by TABLE_NAME
having count(distinct COLUMN_NAME)=2
Below query will give you desired results:
;WITH cte
AS (SELECT object_id , ROW_NUMBER() OVER(PARTITION BY object_id
ORDER BY object_id) AS rn
FROM sys.columns AS c
WHERE c.name = 'USERID'
OR
c.name = 'DOB')
SELECT s.name + '.' + t.name
FROM sys.tables AS t INNER JOIN cte AS c ON t.object_id = c.object_id
INNER JOIN sys.schemas AS s ON t.schema_id = s.schema_id
WHERE c.rn > 1;
Please note to use exact column names with = operator and not like operator to get the correct results.

How do I dynamically join one table to multiple other tables in SQL Server?

I am currently searching my database for all column name's that have a particular substring using the below code.
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
INTO
#Tables
FROM
sys.tables AS t
INNER JOIN
sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE
c.name LIKE '%foo%'
ORDER BY
schema_name, table_name;
I want to take the table and column names this query returns and join each of them to another table like below.
SELECT *
FROM #ReferenceTable rt
INNER JOIN #Tables.table_name t ON rt.ReferenceColumn = t.ColumnName
I believe this can be done through a dynamic query, but I haven't been able to figure out how. My initial thought was to pass a table into sp_executesql, but I'm not sure that's possible. What is the correct way to join multiple table names to another table?

SQL query for a database scheme

In SQL Server how do you query a database to bring back all the tables that have a field of a specific name?
The following query will bring back a unique list of tables where Column_Name is equal to the column you are looking for:
SELECT Table_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_Name = 'Desired_Column_Name'
GROUP BY Table_Name
SELECT Table_Name
FROM Information_Schema.Columns
WHERE Column_Name = 'YourFieldName'
I'm old-school:
SELECT DISTINCT object_name(id)
FROM syscolumns
WHERE name = 'FIELDNAME'