Use table metadata for select statement in SQL Server? - sql

I have a large database and would like to select table names that have a certain column name. I have done something like this in MySQL, but can't find any info on SQL Server.
I want to do something like:
select [table]
from [db]
where table [has column 'classtypeid']
How can I do something like this?

Use the ANSI information_schema views, this will also work in MySQL
select table_name
from information_schema.columns
where column_name = 'classtypeid'

Here you go:
SELECT C.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS AS C
INNER JOIN INFORMATION_SCHEMA.TABLES AS T ON C.TABLE_NAME = T.TABLE_NAME
AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
WHERE C.COLUMN_NAME = 'classtypeid'
AND T.TABLE_TYPE = 'BASE TABLE'
Edit: Note that this will not list views based on any tables with that column. If you only query INFORMATION_SCHEMA.COLUMNS you will also get back views.

Related

Find columns that are one table and but not in the other table

I am working with two very wide tables (Table A, Table B) in Snowflake. Both tables should have exact same columns, though not necessarily in the same order. However Table B is missing a few columns. Is there a way to quickly find all the columns that are in Table A but not in Table B? I am not concerned with the Data, just the existence of columns.
Essentially, I am looking for a SQL way to find all columns in Table that are missing in Table B.
I currently use a ETL tool for this. But it is rather cumbersome. It would be nice if this can be done using SQL.
Try the following
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'tableA'
AND column_name NOT IN
(
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'tableB'
);
You can do this with aggregation:
SELECT column_name
FROM information_schema.columns
WHERE table_name IN ('tableA', 'tableB')
GROUP BY column_name
HAVING MIN(table_name) = MAX(table_name) AND MIN(table_name) = 'TableA';
This is a handy structure that can more easily generalize to multiple tables. It does assume that the two tables are on the same server.
Below query also gives missing columns in Table B.
SELECT column_name FROM information_schema.columns WHERE table_name = 'TableA'
MINUS
SELECT column_name FROM information_schema.columns WHERE table_name = 'TableB'

How to compare two database for unique tables?

I have an old SQL 2008 backup for our application. Today we encountered a case where we want to restore it as a current database in our sql-2012. I used the below query to count the tables in both database
USE YOURDBNAME
SELECT COUNT(*) from information_schema.tables
WHERE table_type = 'base table'
my current database returned total 543 tables whereas old database
showed 533 tables
How to find the unique ten tables from the current database by comparing it with the tables of my previous database. ?
Update 1
I used the below query to compare the tables but it returned no result
select 'dbtest01' as dbname, t1.table_name
from dbtest01.[INFORMATION_SCHEMA].[tables] t1
where table_name not in (select t2.table_name
from
dbtest02.[INFORMATION_SCHEMA].[tables] t2
)
union
select 'dbtest02' as dbname, t1.table_name
from dbtest02.[INFORMATION_SCHEMA].[tables] t1
where table_name not in (select t2.table_name
from
dbtest01.[INFORMATION_SCHEMA].[tables] t2
)
This query includes table_schema to make sure if the table name is repeated but in a different schema then it shows up. This shows tables that are in db_1 but not in db_2 as well as tables in db_2 but not in db_1.
SELECT
db1.dbname AS db1_name,
db1.table_schema AS db1_schema,
db1.table_name AS db1_table,
db2.dbname AS db2_name,
db2.table_schema AS db2_schema,
db2.table_name AS db2_table
FROM (
SELECT
'YOURDBNAME' AS dbname,
table_schema,
table_name
FROM YOURDBNAME.information_schema.tables
WHERE table_type = 'BASE TABLE'
) AS db1
FULL OUTER JOIN (
SELECT
'YOUROTHERDBNAME' AS dbname,
table_schema,
table_name
FROM YOUROTHERDBNAME.information_schema.tables
WHERE table_type = 'BASE TABLE'
) AS db2 ON
db1.table_schema = db2.table_schema
AND db1.table_name = db2.table_name
WHERE db1.dbname IS NULL
OR db2.dbname IS NULL
These two queries will do the work, as a quick ad-hoc query. You have to do it twice, in "both directions", to catch all possible differences.
-- All tables in dbtest01 that are not in dbtest02
SELECT schema_name(schema_id), name
from dbtest01.sys.tables
except select schema_name(schema_id), name
from dbtest02.sys.tables
order by name
-- All tables in dbtest02 that are not in dbtest01
SELECT schema_name(schema_id), name
from dbtest01.sys.tables
except select schema_name(schema_id), name
from dbtest02.sys.tables
order by name
(Updated with schemas, based on #Andrew's reply. Full outer joins work too, but they hurt my brain.)
SELECT table_name from dbtest01.information_schema.tables
WHERE table_type = 'base table'
EXCEPT
SELECT table_name from dbtest02.information_schema.tables
WHERE table_type = 'base table'

Need to find specific column name from database

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';

List names of all tables in a SQL Server 2012 schema

I have a schema in SQL Server 2012.
Is there a command that I can run in SQL to get the names of all the tables in that schema that were populated by user?
I know a similar query for MySQL SHOW TABLES; but this does not work with SQL Server.
Your should really use the INFORMATION_SCHEMA views in your database:
USE <your_database_name>
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
You can then filter that by table schema and/or table type, e.g.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
SELECT t.name
FROM sys.tables AS t
INNER JOIN sys.schemas AS s
ON t.[schema_id] = s.[schema_id]
WHERE s.name = N'schema_name';
SQL Server 2005, 2008, 2012 or 2014:
SELECT * FROM information_schema.tables WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA = 'dbo'
For more details:
How do I get list of all tables in a database using TSQL?
SELECT t1.name AS [Schema], t2.name AS [Table]
FROM sys.schemas t1
INNER JOIN sys.tables t2
ON t2.schema_id = t1.schema_id
ORDER BY t1.name,t2.name
SELECT *
FROM sys.tables t
INNER JOIN sys.objects o on o.object_id = t.object_id
WHERE o.is_ms_shipped = 0;
When you want just the name of the table the easiest way is I guess just doing this:
SELECT * FROM INFORMATION_SCHEMA.TABLES
when need like full name with schema and table name , than just do like this:
SELECT TABLE_SCHEMA + '.' + TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
select * from [schema_name].sys.tables
This should work. Make sure you are on the server which consists of your "[schema_name]"

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'