Locating two columns within a table in an SQL database - sql

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

Related

SQL Server - Number of records from all specific columns at sys.tables

I am trying to find out the number of records from all the columns at sys.tables that start with a specific letter.
At the moment I have something like this:
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 'u%'
ORDER BY TableName
,ColumnName;
Is there a way I can add the number of column records to this?
Tks,
Miguel
Try this:
SELECT c.name AS 'ColumnName'
,t.name AS 'TableName',
count(*) AS num
FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE 'u%'
GROUP BY c.name, t.name
ORDER BY TableName
,ColumnName;

T-SQL :: eliminate doubles from JOIN

I'm writing a query in T-SQL that will allow me to list:
Tables
Columns
Data Type
Rows
but when I run it I noticed that there are double columns.
I don't understand why. I know I can remove them but I believe I'm doing something in my JOIN.
select
sys.tables.name as Table_Name,
sys.columns.name as Column_Name,
sys.types.name as data_type,
sys.partitions.rows as [Rows]
from
sys.columns
right join sys.tables
on sys.columns.object_id = sys.tables.object_id
right join sys.types
on sys.columns.system_type_id = sys.types.system_type_id
inner join sys.partitions
on sys.tables.object_id = sys.partitions.object_id
where
sys.columns.name is not NULL
and
sys.types.name != 'sysname'
order by
Table_Name
To eliminate duplicate rows from your query, the first thing I would try is to SELECT DISTINCT rows, like this:
SELECT DISTINCT --> added an important keyword here
sys.tables.name AS Table_Name,
sys.columns.name AS Column_Name,
sys.types.name AS data_type,
sys.partitions.rows AS [Rows]
FROM
sys.columns
RIGHT JOIN sys.tables ON sys.columns.object_id = sys.tables.object_id
RIGHT JOIN sys.types ON sys.columns.system_type_id = sys.types.system_type_id
INNER JOIN sys.partitions ON sys.tables.object_id = sys.partitions.object_id
WHERE
sys.columns.name IS NOT NULL
AND
sys.types.name <> 'sysname'
ORDER BY
Table_Name
Cause of problem in sys.partitions:
In your "query" used sys.partitions and it is problem reason. In sys.partitions table, the information is stored in the number of indexes of each table, and if a table has 2 or more indexes, it will cause the information to be repeated in your join.

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.

Find number of rows of each column name [duplicate]

This question already has answers here:
Query to list number of records in each table in a database
(23 answers)
Closed 3 years ago.
I wrote this short script which finds a specific column name in a SQL Server database:
SELECT
sys.columns.name AS ColumnName,
tables.name AS TableName
FROM
sys.columns
JOIN
sys.tables ON sys.columns.object_id = tables.object_id
WHERE
sys.columns.name LIKE '%ColumnName%'
I want to add another resulting column where number of rows of each column.
Assuming that there aren't any partitioned table:
USE [SpecificDatabank]
SELECT
sys.columns.name AS ColumnName,
sys.tables.name AS TableName,
sys.partitions.rows AS [Rows]
FROM
sys.columns
JOIN sys.tables ON
sys.columns.object_id = sys.tables.object_id
JOIN sys.partitions ON
sys.tables.object_id = sys.partitions.object_id and sys.partitions.index_id in (0,1)
WHERE
sys.columns.name LIKE '%ColumnName%'
SELECT
sys.columns.name AS ColumnName,
tables.name AS TableName, Totals.Total
FROM
sys.columns
JOIN sys.tables ON
sys.columns.object_id = tables.object_id
JOIN
(SELECT COUNT(*) total, sys.columns.name FROM
sys.columns
JOIN sys.tables ON
sys.columns.object_id = tables.object_id
GROUP BY sys.columns.name
) Totals
on Totals.name = sys.columns.name
WHERE
sys.columns.name LIKE '%ColumnName%'
This returns the number of times a column exists across all of the tables

How to get list of all tables that has identity columns

I would like to learn how to fetch list of all tables that has identity columns from a MS SQL database.
SELECT
[schema] = s.name,
[table] = t.name
FROM sys.schemas AS s
INNER JOIN sys.tables AS t
ON s.[schema_id] = t.[schema_id]
WHERE EXISTS
(
SELECT 1 FROM sys.identity_columns
WHERE [object_id] = t.[object_id]
);
The select itself is very straightforward, using the ANSI system of INFORMATION_SCHEMA views.
The obscure part is the COLUMNPROPERTY function. The function retrieve one property associated with a column. In this case the IsIdentity property, which marks if a column uses the identity property
select COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
I like this approach because it uses a join instead of a WHERE EXISTS or a call to COLUMNPROPERTY. Note that the group by is only necessary if you a) have tables with more than one IDENTITY column and b) don't want duplicate results:
SELECT
SchemaName = s.name,
TableName = t.name
FROM
sys.schemas AS s
INNER JOIN sys.tables AS t ON s.schema_id = t.schema_id
INNER JOIN sys.columns AS c ON t.object_id = c.object_id
INNER JOIN sys.identity_columns AS ic on c.object_id = ic.object_id AND c.column_id = ic.column_id
GROUP BY
s.name,
t.name
ORDER BY
s.name,
t.name;
The script below will do:
SELECT a.name as TableName,
CASE WHEN b.name IS NULL
THEN 'No Identity Column'
ELSE b.name
END as IdentityColumnName
FROM sys.tables a
LEFT JOIN sys.identity_columns b on a.object_id = b.object_id
Old post, I know, but.. if you're in the same database as what you're trying examine the tables in (like all the other scripts on this thread do), you can avoid all explicit joins by turning to some very useful functions. Comment out the DataType line and the ,* line if you don't need them.
SELECT SchemaName = OBJECT_SCHEMA_NAME(object_id)
,ObjectName = OBJECT_NAME(object_id)
,DataType = TYPE_NAME(system_type_id)
,*
FROM sys.identity_columns
;
Select OBJECT_NAME(object_Id) Rrom sys.identity_columns
where is_identity = 1;