Get all information schema from Database tables - sql-server

How can I limit the following query to return for only tables in the database and exclude views?
USE [Database Name] SELECT * FROM INFORMATION_SCHEMA.COLUMNS

Microsoft discourages the use of INFORMATION_SCHEMA. The way to do it if you don't want to use the sys views:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS AS C
INNER JOIN INFORMATION_SCHEMA.TABLES AS T
ON T.TABLE_SCHEMA = C.TABLE_SCHEMA
AND T.TABLE_NAME = C.TABLE_NAME
WHERE T.TABLE_TYPE = 'BASE TABLE'

Join it to sys.Tables:
SELECT c.*
FROM INFORMATION_SCHEMA.COLUMNS c inner join sys.tables t
on c.Table_Name = t.name

Related

List down the column names in a Temporary Table in SQL Server

I am a beginner to SQL Server Database. I have a basic question.
How to retrieve the column names of a temporary table in SQL Server?
I have tried querying the sys.objects, but the table is not listing there.
Temp tables are stored in the tempdb database so you have to query the views from the tempdb database. Something like this:
SELECT c.name
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c ON c.object_id = o.object_id
WHERE o.name LIKE '#TempTableName%';
SELECT Obj.NAME
,Col.NAME
FROM tempdb.sys.objects Obj
INNER JOIN tempdb.sys.columns Col ON Obj.object_id = Col.object_id
WHERE Obj.NAME LIKE '#tmp%'
But please note that the local temporary table names will not be unique. We can have the same names from different sessions. So be careful with the query.
Here's how to get the temp table for just the current session.
SELECT c.name
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c ON c.object_id = o.object_id
WHERE o.object_id = object_id('tempdb..#TempTableName)
`SELECT STUFF(
`(SELECT ',' + LTRIM(RTRIM(ISNULL(name,'')))
`FROM tempdb.sys.columns
`WHERE Object_ID = Object_ID(N'tempdb..#TEMP')
`FOR XML PATH(''), TYPE).value('.','varchar(max)'), 1, 1, '')
`AS comma_separated_list

How to select database tables where they have two or more specific columns in them?

How to select database tables where they have two or more specific columns in them?
The one here displays tables with CustomerName in them. To add another column to look for, it seems I might have to add another subquery within the subquery.
I am looking for a simple query where I can add x number of columns without adding complexity.
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 t.name in (select t.name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID where c.name LIKE '%CustomerID%')
ORDER BY schema_name, table_name;
UPDATE
Would want to use the LIKE operator
Assuming you want tables that contain ALL the column names you provide, an easy way without subqueries could be:
SELECT t.name AS table_name,
SCHEMA_NAME(t.schema_id) AS schema_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name IN('COL1', 'COL2') -- Column names in here
GROUP BY t.name, SCHEMA_NAME(t.schema_id)
HAVING COUNT(DISTINCT c.name) = 2 -- Match the count of columns in the IN criteria
ORDER BY SCHEMA_NAME(t.schema_id), t.name;

Retrieving the name of columns of one table from a SQL Server database

Is it possible to retrieve the list of the name of the columns of a table in a SQL Server database?
This should work with any version of SQL Server.
SELECT c.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_SCHEMA = 'schemaName'
AND c.TABLE_NAME = 'tableName'
ORDER BY c.ORDINAL_POSITION
You can press alt+f1 while the table name is selected, this does the same thing as:
sp_help #objname= 'table_name'
Try this:
SELECT
c.name 'Column Name',
FROM
sys.columns c
INNER JOIN
sys.types t ON c.system_type_id = t.system_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
c.object_id = OBJECT_ID('YourTableName')
OR THIS:
SELECT SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
WHERE t.name = 'YourTableName'
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY schema_name, table_name;
Beginning with SQL Server 2005 (and up through SQL Server 2012, as of the writing of this answer), the sp_columns stored procedure can provide you with the list of columns (plus a bunch of other stuff too!) for a specific table. The returned result set will include the COLUMN_NAME column, which has what you are looking for.
To use it, run the following statement in your database:
EXECUTE sp_columns #table_name='YourTableName'
Microsoft's online help for sp_columns can be found here.

SQL Server Schema Output

How do I output the schema of my database? I want it to output the design of the database.
Something like this might work:
SELECT TABLE_TYPE, TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY TABLE_TYPE, TABLE_NAME, COLUMN_NAME
But I can't get it to run correctly. A excel file with table names, their columns, types, primary keys, etc. is what I want.
SELECT * FROM INFORMATION_SCHEMA.TABLES;
... sounds like what you are looking for, sans any formatting you want to do.
select
t.type_desc,
t.name as [table],
c.name as [column],
y.name,
c.max_length
from sys.tables t inner join
sys.columns c on c.object_id = t.object_id inner join
sys.types y on c.system_type_id = y.system_type_id
where y.name <> 'sysname'
order by
t.type_desc,
t.name,
c.name
Below posts would be useful for generating data dictionary
Database Documentation - http://deepakrangarajan.blogspot.com/2011/03/database-documentation.html
Generating a Database Data Dictionary - http://sqlserverdiaries.com/blog/index.php/2011/02/generating-a-database-data-dictionary/

SQL Server INFORMATION_SCHEMA contents

In most RDBMS, the meta-model is "self contained", which means that I can find out the model of the meta-model by browsing the meta-model itself. This doesn't seem to be the case with SQL Server. What I want to do is this:
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'INFORMATION_SCHEMA'
That way, I can discover the INFORMATION_SCHEMA schema itself.
Is there any grant/permission/login setting that I have to configure in order to make the INFORMATION_SCHEMA views be "self contained"?
Don't think this is possible.
The definition of the INFORMATION_SCHEMA.TABLES view is
CREATE VIEW [INFORMATION_SCHEMA].[TABLES]
AS
SELECT
DB_NAME() AS TABLE_CATALOG,
s.name AS TABLE_SCHEMA,
o.name AS TABLE_NAME,
CASE o.type
WHEN 'U' THEN 'BASE TABLE'
WHEN 'V' THEN 'VIEW'
END AS TABLE_TYPE
FROM
sys.objects o LEFT JOIN sys.schemas s
ON s.schema_id = o.schema_id
WHERE
o.type IN ('U', 'V')
so it pulls its information from sys.objects however this in turn contains nothing about the INFORMATION_SCHEMA objects.
The metadata for these is accessed via sys.system_objects instead.
You can use sys.all_views
select SCHEMA_NAME(schema_id), name
from sys.all_views
order by 1,2
USE information_schema;
SHOW TABLES;
USE mysql;
SHOW TABLES ;

Resources