search for multiple columns in a single table sql - 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.

Related

How to get records from column_name

I need help in SQL Server, I want to get the number of identical values from a column, but I only have column names, here is my script
select
column_name, table_name, data_type, d.name, d.description
from
INFORMATION_SCHEMA.COLUMNS
inner join
Dims d on table_name = 'Dim_' + d.name + '_View'
where
column_name = 'ExtCode'
group by
column_name, table_name, data_type, d.name, d.description
having
count(column_name) > 1;
I want to get the number of records in column names that are greater than 1
I think you are trying to do two things with one SQL:
Find all the column names that are repeated across tables
Display each occurrence of it along with the table they occur in. Break this up into two parts.
Start with a list of repeated column names:
Select column_name From INFORMATION_SCHEMA.COLUMNS
Where table_name like 'Dim%View'
Group By column_name Having count(*)>1
Then join that to your query to retain only the columns that appear more than once:
select
Cols.column_name, cols.table_name, cols.data_type, d.name, d.description
from INFORMATION_SCHEMA.COLUMNS cols inner join Dims d
on table_name = 'Dim_' + d.name + '_View'
Inner Join (
Select column_name From INFORMATION_SCHEMA.COLUMNS
Where table_name like 'Dim%View'
Group By column_name Having count(*)>1
) multi On multi.column_name=cols.column_name

How to use INFORMATION_SCHEMA to find common fields between 2 tables?

For example, if I need to join TableA & TableB - how can I use the INFORMATION_SCHEMA database to find the common field between them?
Here is one method:
select column_name
from information_schema.columns c
where table_name in ('A', 'B')
group by column_name
having count(*) = 2;
If necessary, you should also include table_schema to identify the tables.
Another way to get common fields in two table:
SELECT C.name
FROM SYS.OBJECTS O
JOIN SYS.COLUMNS C
ON O.object_id = C.object_id
WHERE O.name IN
(
'TABLE-A', 'TABLE-B'
)
group by C.name
having count(*) = 2;
Way to get common column in two table:
SELECT A.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS A
JOIN INFORMATION_SCHEMA.COLUMNS B ON A.COLUMN_NAME = B.COLUMN_NAME
WHERE A.TABLE_NAME 'Table_1'
AND B.TABLE_NAME = 'Table_2'

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?

Finding all tables with matching columns

I have 81 tables where I want to find matching columns and output a list like:
"columnName" found in 3 tables:
table1
table2
table3
"columnName2" found in 4 tables:
table1
table3
table4
table5
The table INFORMATION_SCHEMA.COLUMNS is a system table which contains all columns for all tables. You can look at it by selecting from it.
For your question, you should try this :
select I.Column_name,
table_name,
table_count
from INFORMATION_SCHEMA.COLUMNS I
inner join
(
Select Column_name,
count(*) as table_count
from INFORMATION_SCHEMA.COLUMNS
group by Column_name) as T on T.Column_name = I.Column_name
You will have a table with, for each column_name, the tables, and the count in the column table_count.
I ust don't know how to output a list. You should put it on excel then if you want to format it I guess ..
Tell me if you have problem !
Below Query should give you the desired result. Let me know if you face any issues. You can modify script if you want to be specific about the single database
This query provides you Column Name - Concatinated Table Names - Number of Tables
SELECT t.CNAME AS ColumnName, STUFF(
(SELECT ',' + S.TNAME
FROM
(
SELECT C.NAME AS CNAME , T.NAME AS TNAME
FROM SYS.OBJECTS AS T
JOIN SYS.COLUMNS AS C
ON T.OBJECT_ID=C.OBJECT_ID
WHERE T.TYPE_DESC='USER_TABLE'
)s
WHERE s.CNAME = t.CNAME
FOR XML PATH('')),1,1,'') AS TablesUsed,
COUNT(t.TNAME)
FROM
(
SELECT C.NAME AS CNAME , T.NAME AS TNAME
FROM SYS.OBJECTS AS T
JOIN SYS.COLUMNS AS C
ON T.OBJECT_ID=C.OBJECT_ID
WHERE T.TYPE_DESC='USER_TABLE'
)t
GROUP BY t.CNAME
HAVING COUNT(t.TNAME) > 1
ORDER BY COUNT(t.TNAME) DESC

Locating two columns within a table in an SQL database

I use this query frequently to retrieve all columns matching a string in a database. I am trying to link two tables and to do so I need matching columns to connect the two.
What I want to be able to search for is a table that has a column with the string "CUSTOMER_ID" AND another column with "WORKORDER_BASE_ID"
Is there an easy way to do this, working from this sqript? I thought some type of subquery might be necessary, but I have yet to make anything work.
SELECT
t.name AS table_name
,SCHEMA_NAME(schema_id) AS schema_name
,c.name AS column_name
,T.modify_date
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE
c.name LIKE '%CUSTOMER_ID%'
ORDER BY modify_date
Something like this should work:
SELECT t.name AS table_name ,
SCHEMA_NAME(schema_id) AS schema_name ,
c.name AS column_name ,
T.modify_date
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE EXISTS ( SELECT *
FROM sys.columns c1
WHERE c1.object_id = c.object_id
AND c1.name = 'CUSTOMER_ID' )
AND EXISTS ( SELECT *
FROM sys.columns c1
WHERE c1.object_id = c.object_id
AND c1.name = 'WORKORDER_BASE_ID' )
ORDER BY modify_date