Accessing Database Meta Data - sql

Im running into a tough issue. I have a database using Microsoft SQL 2008 and in this database there are many tables. The tables were auto generated and do not have meaningful names. There is one particular table that I need, and I can not seem to find it.
I know what the names of a few of the columns in the table are called. Is there a way I can go through all the tables one at a time looking at the names of the columns and seeing if they match the ones I know.
If they do, then I can look farther into it the table to see if it is the one I am looking for. Does this sound like a good approach to the problem? Is it possible? Any ideas of where to start?

SELECT OBJECT_SCHEMA_NAME([object_id]),
OBJECT_NAME([object_id])
FROM sys.columns
WHERE name IN ('column 1', 'column 2'
/* , ... other columns */);
EDIT by request, in case the OP meant to identify ALL vs. ANY:
SELECT OBJECT_SCHEMA_NAME([object_id), name
FROM sys.tables AS t
WHERE EXISTS
(
SELECT 1 FROM sys.columns
WHERE name = 'column 1'
AND [object_id] = t.[object_id]
)
AND EXISTS
(
SELECT 1 FROM sys.columns
WHERE name = 'column 2'
AND [object_id] = t.[object_id]
)
/* ... repeat for other columns ... */

Alternative to Aaron's answer using Information_schema.columns instead of sys.columns
SELECT Table_name
FROM
information_schema.columns
WHERE
column_name IN ('column 1', 'column 2')
GROUP BY Table_Name
Having COUNT(column_name) = 2
See this Data.SE query for a working example

With the scripts above, you are limited to SQL wild-carding, which can be pretty limited. You can use SchemaCrawler grep to more powerfully search through your database using regular expressions. SchemaCrawler also allows you additional features to to look for tables related by foreign keys, so for example, you can say find me all tables that have a customer address column, along with the tables that refer to these tables. SchemaCrawler is a command-line tool that is bundled with a Microsoft SQL Server database driver.
Sualeh Fatehi, SchemaCrawler

Related

How to find identical columns in multiple tables in SQL

Is there a simpler way to find matching column names in multiple tables?
The only way I know how to do this currently is to check each table individually but some tables have a bunch of columns and I know my human eye can miss things.
For SQL Server:
SELECT c.name, string_agg(t.name, ', ')
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
group by c.name
Use information_schema.columns. For instance, to get all column names in multiple tables:
select column_name, string_agg( concat(table_schema, '.', table_name), ',')
from information_schema.columns
group by column_name
having count(*) > 1;
The information_schema views are actually standard and available in many databases, including SQL Server.
In Oracle
Use DBA_TAB COLUMNS or ALL_TAB_COLUMNS table it contains names of columns with table name and other details
Vladimir's answer is right and specific to SQL Server.
Another good answer is to use INFORMATION_SCHEMA views to retrieve what you're looking for. INFORMATION_SCHEMA is a standard follow by some DBMS to provides a common view for common objects.
https://en.wikipedia.org/wiki/Information_schema
https://learn.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/system-information-schema-views-transact-sql?view=sql-server-ver15

Get column differences between 2 databases (SQL Server)

I've got 2 databases almost identical to one another. However, it seems that for some tables in the new database, they are missing columns that are in the old database.
What would be the best way to see the differences between columns in tables between 2 databases? Specifically, I need to see what columns AREN'T in the new database that ARE in the old one.
I've tried looking this up but most things I found were either not what I needed or looking at "records".
You can query the columns from your db using the sys tables and compare the result sets. This script assumes your old db has all the columns you want.
;WITH old_db_columns AS (
SELECT c.object_id, c.column_id, c.name AS column_name, t.name AS table_name
FROM old_db.sys.tables t
INNER JOIN old_db.sys.columns c
ON t.object_id = c.object_id
)
, new_db_columns AS (
SELECT c.object_id, c.column_id, c.name AS column_name, t.name AS table_name
FROM new_db.sys.tables t
INNER JOIN new_db.sys.columns c
ON t.object_id = c.object_id
)
SELECT *
FROM old_db_columns o
WHERE NOT EXISTS (
SELECT 1
FROM new_db_columns n
WHERE n.table_name = o.table_name
AND n.column_name= o.column_name)
You may use SQL Compare and SQL Data Compare, tools by Red Gate, to compare and sync databases schema and data.
You can generate the create statement of the tables and you can compare them with using any diff tool.
Check out that video: VS Comparision
Visual Studio has built in functionality that you are able to do data compares, schema compares and it will generate the differences for you in a script if you need to recitfy the variances.

How to get column names from a query in SQL Server

Using SQL Server.
I have a very extensive query, with a lot of aliasing, etc...
Is there a way, using just SQL (stored proc is fine, but not PHP, etc), to get a list of all column names from this query? (I realize I will have to probably embed my query inside of this solution but that is fine. Just a temporary measure.)
Thanks!
If you're using SQL Server 2012 or later you can take advantage of sys.dm_exec_describe_first_result_set
SELECT name
FROM
sys.dm_exec_describe_first_result_set
('Your Query Here', NULL, 0) ;
DEMO
There are various ways that you can get the columns out of the query, such as:
select top 0 s.*
from (<your query here>) s;
Then you can parse the results.
However, I have found another approach useful. Create either a view or a table using the same logic:
select top 0 s.*
into _TempTableForColumns
from (<your query here>) s;
Then use information_schema (or the system tables if you prefer):
select *
from information_schema.columns
where table_name = '_TempTableForColumns' and schema_name = 'dbo';
drop table _TempTableForColumns;
The advantage of this approach is that you can get type information along with the column names. But the engine still has to run the query and that might take time even though no rows are returned. Although the column names and types are available after compiling, I am not aware of a way to get them without also executing the query.
After SQL Server 2008
select *
from sys.columns c
inner join sys.objects o on c.object_id = o.object_id
where o.name = 'TableName'
Before
select *
from syscolumns c
inner join sysobjects o on c.id = o.id
where o.name = 'TableName'

Searching for a table based on values contained in the table

I have a table with a reason_id foreign key. I need to map that back to its primary table. I have searched our databases for matching/similar column names but I wasn't able to find the table.
I have a list of values that would be associated with the reason_id. I'd like to search for tables that contain the list I have. Any help would be appreciated.
Here's the query I was running to search for columns:
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 '%reason%'
There is no easy way to find the related data in other tables.
I'd try with tools such as ApexSQL Search or SQL Search. Both are free and you won’t go wrong with any of these.
If you want to do it with SQL only then identify all columns in all tables that have the same data type. To do so use sys.columns, sys.types and sys.tables views. Once you find all columns just try start writing queries for each table until you find the right one.
I’d go with something like this
select COUNT(*)
from tableX
where tableX.matchedColumn in
(
-- take 100 or more random rows from the original table
-- if result gives you a number that is near to the number of values listed here then you are most probably on the right track
)

Given a column name how can I find which tables in database contain that column?

Given a column name how can I find
which tables in database contain that
column ?
or alternatively
How can I find that particular column
exists for all tables in Database ?
Note: Kindly explain answers with Examples as that I get most knowledge from the answer.
Edit: I am using MySQL Database.
SELECT * FROM information_schema.columns WHERE COLUMN_NAME = 'mycolumn'
Depends on the database you are using. Many database systems expose a set of tables of views that contain details of the schema. For example, you can get schema information from the SYSTABLE and SYSCOLUMN views in Sybase ASA.
in SQL Server:
select distinct t.name
from sys.Columns c
inner join sys.tables t on c.object_id = t.object_id
where c.name = 'YOUR_COLUMNNAME'