Find multiple tables with duplicate data in sql server - sql-server

I have many tables in my database. I want to create a general query which will search for duplicate records in all columns of all the tables in a database in SQL server.
Something like this,
select
T.NAME as TABLE_NAME,
C.NAME as COLUMN_NAME
from
SYS.TABLES as T
inner join
SYS.COLUMNS C on T.OBJECT_ID = C.OBJECT_ID
group by
T.NAME, C.NAME
having
count(*) > 1
I do not know how to do it or if there is any way to do it.

You can find duplicate column names with something like this:
select column_name
from information_schema.columns
group by column_name
having count(*) > 1;
You can join back to column to get the table names. You might also want to limit the search to base tables (it is unclear what you mean by "duplicates").

If you want a quick way to see if two tables are the same you can start with CHECKSUM_AGG and compare the results against two tables. If they have the same columns types and the results of checksum_agg are equal the contents are the same regardless of the row order.

Related

How to get columns that are repeating in different tables in a database inside SQL Server [duplicate]

This question already has answers here:
How can I get column names from a table in SQL Server?
(22 answers)
Closed 3 years ago.
I've a database with huge number of tables. From these tables I want to list out all the column names that are appearing in more than one table along with the their table names.
I tried google search to find any suitable article that can explain how to achieve the results that I've described in the problem section.
No code snippets
No error messages.
This will return the column names and the table names:
select distinct
TABLE_NAME,
COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME in(
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS
group by COLUMN_NAME
having count(*) > 1
)
order by TABLE_NAME, COLUMN_NAME
There are a lot of results and snippets that show how to get all column names in a database. It's a single query to sys.columns, eg :
select Name
from sys.columns
The same SQL queries that return multiple occurences in any table can be used here too, eg :
select Name,count(*)
from sys.columns
group by Name
having count(*)>1
Will return column names that occur more than once.
You can get the column and table names without joining by using COUNT() with OVER :
with t as (
select name,
object_name(object_id) as TableName,
count(*) over (partition by name) cnt
from sys.columns
)
select *
from t
where cnt >1
The query below will return the column name along with the number of its occurences in the current database tables:
SELECT c.NAME AS 'ColumnName', COUNT(*) AS Occurences
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
GROUP BY c.NAME
HAVING COUNT(*) > 1
This query below will help to identify the specific column name is placed in which tables:
-- Find column name in all the tables
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 '%columnname%' -- to be searchable column name
ORDER BY TableName, ColumnName;

I want to find column name in my database - is it possible?

My database name is CARE_DynamicsAX and I want to find a column name workerStatus
select * from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'workerStatus'
If I'm not wrong, you are trying to find the Table where you have a column name as workerStatus. If that is the case, you might run this query to find the same.
This works for the column names from TABLES for SQL Server.
This query would run under the assumption that you know that the column name that you are searching starts with workerStat
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 'workerStat%'

How to compare the columns of two tables, SQL Server?

I understand creating a join and comparing the values of specific columns from two tables. In this case, I am only interested in comparing the columns between two different tables, not the values.
I want to check to see if I have the correct number of columns in my new table, and that the spelling of each column in my new table matches the other. Essentially, a way to QC the schema of the new table.
Any suggestions for a SQL query to execute this?
You can utilize the table below:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = SOME DB
AND TABLE_NAME = SOME TBL
Or you can use sys.columns similarly.
You want to check whether the Data Definition is correct. You cannot do so using a Data Query on those tables.
However, SQL Server stores the information about the tables in the database in other tables. See: https://msdn.microsoft.com/en-us/library/ms186778.aspx
In this case you might get what you want using a query on the INFORMATION_SCHEMA.COLUMNS table.
;WITH cteTable1Columns AS (
SELECT
c.object_id
,c.name
FROM
sys.columns c
WHERE
c.object_id = object_id('A')
)
, cteTable2Columns AS (
SELECT
c.object_id
,c.name
FROM
sys.columns c
WHERE
c.object_id = object_id('B')
)
SELECT
OBJECT_NAME(c1.object_id) as Table1Name
,c1.name as Table1ColName
,OBJECT_NAME(c2.object_id) as Table2name
,c2.name as Table2ColName
FROM
cteTable1Columns c1
FULL JOIN cteTable2Columns c2
ON c1.name = c2.name
WHERE
c1.object_id is NULL
OR c2.object_id IS NULL
If you use a method like this to identify the columns in each table then do a full outer join on name it will show you all of your problems if any exist. (note you can also use INFORMATION_SCHEMA.COLUMNS)

Find an instance of a character in a database

Is there a relatively easy way to find an instance of a character, say a "|" or a "~" or a "&", in a database?
It could be in any field...in any table.
There are around 400 to 500 tables in the database in total.
the easiest way is to bulk export all the data for all of the tables and then search/grep for the string in question.
http://msdn.microsoft.com/en-us/library/ms187042.aspx
You could export the database to one big SQL script using the SQL Management Console. Once you have the text file you can use notepad/grep/etc. to find the characters you're looking for.
This code will identify each table and varchar column in your database. It could be used to generate a select statement for each table and column to search for rows where that column contains the characters you're looking for.
select o.id, o.name, c.name, t.name, t.*
from sysobjects o
join syscolumns c on c.id = o.id
join systypes t on t.xtype = c.xtype
where o.xtype = 'U'
and t.status = 0
and t.name like '%varchar'
order by o.name, c.name
Depending on the size and indexing of your tables this may or may not be a good idea.

SQL Query to search schema of all tables

I am working on a SQL Server 2008 Db that has many tables in it (around 200). Many of these tables contain a field by the name "CreatedDate". I am trying to identify all the table schema with this particular field.
Is there a SQL query to do this?
I would query the information_schema - this has views that are much more readable than the underlying tables.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%create%'
You can also try doing this using one of many third party tools that are available for this.
Queries are great for simple searches but if you need to do more manipulation with data, search for references and such this is where you can do a much better job with these.
Also, these come in very handy when some objects are encrypted and you need to search for
I’m using ApexSQL Search which is free but there are also many more (also free) on the market such as Red Gate or SSMS Tool Pack.
select object_name(c.object_id) as table_name
, schema_name(t.schema_id) as schema_name
from sys.columns c
join sys.tables t on c.object_id = t.object_id
where c.name=N'CreatedDate';
It gets a little more complicated if you want alsoother table properties, but you'll refer to the object catalog views like sys.tables, sys.columns etc.
My favorite...
SELECT objParent.name AS parent, obj.name, col.*
FROM sysobjects obj
LEFT JOIN syscolumns col
ON obj.id = col.id
LEFT JOIN sysobjects objParent
ON objParent.id = obj.parent_obj
WHERE col.name LIKE '%Comment%'
OR obj.name LIKE '%Comment%'
Above I'm searching for "Comment".
Drop the percent signs if you want a direct match.
This searches tables, fields and things like primary key names, constraints, views, etc.
And when you want to search in StoredProcs after monkeying with the tables (and need to make the procs match), use the following...
SELECT name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Comment%'
Hope that helps, I find these two queries to be extremely useful.
For me I only have read access to run querys so I need to use this function often here is what I use:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where TABLES.TABLE_NAME like '%your table name here%'
You can replace .TABLES with .COLUMNS then it would look like this:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE columns.COLUMN_NAME like '%your column name here%'
Use this query :
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name , *
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
Where
( c.name LIKE '%' + '<ColumnName>' + '%' )
AND
( t.type = 'U' ) -- Use This To Prevent Selecting System Tables
Same thing but in ANSI way
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME IN (
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME = 'CreateDate'
)
You do not need to type SQL Query for this in SQL Server 2008.
In SSMS Object Explorer choose Databases or Tables of the required database (if you need to search in one database), open menu View--> Object Explorer Details (alternatively press F7), type %CreatedDate% in Search textbox, ENTER, enjoy

Resources