System Versioned Table check schema from sys.tables - sql-server

How to check if a table has versioning enabled by specifying not only the table name but also the schema.
I use:
IF EXISTS (SELECT 1 FROM sys.tables
WHERE NAME = 'tablename'
AND temporal_type = 2)
SELECT 1 AS TABLE_EXIST
ELSE
SELECT 0 AS TABLE_EXIST
Regards

You can JOIN sys.schemas and filter on the name column from both tables:
IF EXISTS (
SELECT 1
FROM sys.tables t
INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
WHERE temporal_type = 2
AND s.name = 'schemaname'
AND t.name = 'tablename'
)
SELECT 1 AS TABLE_EXIST
ELSE
SELECT 0 AS TABLE_EXIST;

You can use the SCHEMA_ID function, which exists to simplify queries like this:
SELECT *
FROM SYS.tables
WHERE
NAME = 'tablename'
and schema_id = schema_id('dbo')
and temporal_type = 2

Related

Find tables for which specific column not exists sql server

How to find tables in which specific column not exists.
E.g column ID not present in table Contact, then Contact table will be in result set.
I tried:
SELECT DISTINCT t.name
FROM sys.tables t
INNER join sys.columns C ON c.object_id = t.object_id
WHERE c.name <> 'ID'
But above query return all table for which column ID is present in it.
Can u try this query,
SELECT * FROM sys.tables WHERE type = 'U' AND object_id NOT IN (
SELECT DISTINCT c.object_id FROM sys.columns c WHERE c.name = 'ID')
SELECT name
FROM sys.Tables
WHERE Object_ID NOT IN (SELECT Object_ID FROM sys.Columns WHERE name = 'ID')
You don't need to perform a join, you can just add a subquery that identifies tables that have an ID column and exclude the object_id's using NOT IN(..the subquery..):
SELECT DISTINCT t.name
FROM sys.tables t
WHERE t.object_id NOT IN (SELECT object_id FROM sys.columns WHERE name = 'id')
The reason your query doesn't work is because you are simply getting all columns that aren't = ID and each table has many columns that match that criteria so they will be returned even if there is an ID column.
One method is with NOT EXISTS:
SELECT OBJECT_SCHEMA_NAME(t.object_id) AS SchemaName
, t.name AS TableName
FROM sys.tables t
WHERE NOT EXISTS ( SELECT *
FROM sys.columns c
WHERE c.object_id = t.object_id
AND c.name = 'ID' );
Try the following, lists all table names NOT having ID.
SELECT tableName
FROM (
SELECT DISTINCT
t.name AS tableName
,c.name AS columnName
FROM sys.tables t
INNER JOIN sys.columns C
ON c.object_id = t.object_id
) AS t
WHERE columnName NOT IN( 'ID')
GROUP BY tableName
If you down't want to see system tables, then..
SELECT DISTINCT OBJECT_NAME(OBJECT_ID) TableName FROM SYS.COLUMNS WHERE NAME <> 'id'
and OBJECTPROPERTY (OBJECT_ID, 'IsUserTable') = 1
EXCEPT
SELECT DISTINCT OBJECT_NAME(OBJECT_ID) TableName FROM SYS.COLUMNS WHERE NAME = 'id'

Calculate number of primary and secondary key columns

Is there a tool or query that will give me output as the total number of primary and secondary keys in a database table?
UPDATE ANSWER
Please note that secondary key is different from foreign key.
So count of no. of primary keys is possible in 2 ways:-
SELECT *
FROM sys.indexes i
INNER JOIN sys.tables t ON i.object_id = t.object_id AND
t.type = 'U'
LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
where is_primary_key=1
OR
SELECT COUNT(*) AS 'PRIMARY_KEY_CONSTRAINT'
FROM sys.objects
WHERE type_desc IN ('PRIMARY_KEY_CONSTRAINT')
To count no. of secondary keys use:
SELECT *
FROM sys.indexes i
INNER JOIN sys.tables t ON i.object_id = t.object_id AND
t.type = 'U'
LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
where is_unique=1 and is_primary_key=0
USE database
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName
FROM sys.objects
WHERE type_desc IN ('PRIMARY_KEY_CONSTRAINT')
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName
FROM sys.objects
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT')
Simple count
SELECT COUNT(*) AS 'PRIMARY_KEY_CONSTRAINT'
FROM sys.objects
WHERE type_desc IN ('PRIMARY_KEY_CONSTRAINT')
SELECT COUNT(*) AS 'FOREIGN_KEY_CONSTRAINT'
FROM sys.objects
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT')
The tool is SQL and this is another method to get what you want. A bit more complex then #kevchadders, but this way you can also make a listing of column names, types, etc
Count Foreign keys
SELECT count(*) from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type in ( 'FOREIGN KEY')
AND Col.Table_Name in (select name from sysobjects where xtype = 'U')
Count Pirmairy keys
SELECT count(*) from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type in ('PRIMARY KEY')
AND Col.Table_Name in (select name from sysobjects where xtype = 'U')
For all tables, show the PK and FK
FK
SELECT *, Col.Column_Name from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type in ('PRIMARY KEY', 'FOREIGN KEY')
AND Col.Table_Name in (select name from sysobjects where xtype = 'U')
You can try this
SELECT 'PRIMARY KEYS' AS KeyType, COUNT(*) AS Total
FROM sys.tables AS TB INNER JOIN
sys.key_constraints AS KC ON KC.parent_object_id = TB.object_id
GROUP BY KC.type
UNION
SELECT 'FOREIGN KEYS' AS KeyType, COUNT(*) AS Total
FROM sys.tables AS TB INNER JOIN
sys.foreign_keys AS FK ON FK.referenced_object_id = TB.object_id
GROUP BY FK.type

How to get table columns according its schema and table name?

I want to get ColumnName and it's datatype and is_identity for each column of different databases for specific table and schema.
I use following code but SCHEMA_NAME is for current database but I want to run this query from master or another database.
How to check schema name?
SELECT
c.name AS column_name, tp.name as data_type, c.is_identity iden
FROM
DatabaseName.sys.tables AS t
INNER JOIN
DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN
DatabaseName.sys.types tp ON c.user_type_id = tp.user_type_id
WHERE
t.name = 'PlaceType'
AND SCHEMA_NAME(t.schema_id) = 'Coding'
Instead of SCHEMA_NAME, use OBJECT_SCHEMA_NAME, like so:
SELECT c.name AS column_name,tp.name as data_type,c.is_identity iden FROM
DatabaseName.sys.tables AS t INNER JOIN
DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID INNER JOIN
DatabaseName.sys.types tp ON c.user_type_id = tp.user_type_id where
t.name='PlaceType' and OBJECT_SCHEMA_NAME(t.object_id,DB_ID(Databasename))='Coding'
Just change the last condition like
and SCHEMA_NAME(t.schema_id) in (Select name from Sys.Databases where database_id > 4)

How to find list of tables having no records in SQL server

How to display a list of tables having no record in them and they are existing in the sql server database.Required is only to show tables with no record in them.
Try this:
SELECT
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.partitions p ON t.object_id = p.OBJECT_ID
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND p.rows = 0
GROUP BY
t.Name, p.Rows
ORDER BY
t.Name
The query goes to the sys.tables and other catalog views to find the tables, their indexes and partitions, to find those tables that have a row count of 0.
Alteration to add Schema names:
SELECT
sch.name,
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.partitions p ON t.object_id = p.OBJECT_ID
inner Join sys.schemas sch
on t.schema_id = sch.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND p.rows = 0
GROUP BY
sch.name,t.Name, p.Rows
ORDER BY
sch.name,t.Name
I found some of the previous answers still returned tables with data for me.
However, the following seems to correctly on report those with no rows (using SQL Server 2019):
SELECT schema_name(tab.schema_id) + '.' + tab.name AS [table]
FROM sys.tables tab
INNER JOIN sys.partitions part ON tab.object_id = part.object_id
WHERE part.index_id IN (
1
,0
) -- 0 - table without PK, 1 table with PK
GROUP BY schema_name(tab.schema_id) + '.' + tab.name
HAVING sum(part.rows) = 0
ORDER BY [table]

How to get all the columns of the MS SQL including view columns as well

I'm trying to write a SQL query that will output all the columns in tables and views.
The query below returns just table columns and doesn't include view 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
ORDER BY schema_name, table_name;
Try this one (fast) -
SELECT
[object_name] = s.name + '.' + o.name
, column_name = c.name
, o.type_desc
FROM sys.objects o
JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
JOIN sys.columns c ON o.[object_id] = c.[object_id]
WHERE o.[type] IN ('U', 'V')
ORDER BY [object_name]
Or try this (slow) -
SELECT
[object_name] = s.name + '.' + o.name
, column_name = c.name
FROM (
SELECT v.name, v.[object_id], v.[schema_id]
FROM sys.views v
UNION ALL
SELECT t.name, t.[object_id], t.[schema_id]
FROM sys.tables t
) o
JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
JOIN sys.columns c ON o.[object_id] = c.[object_id]
ORDER BY [object_name]
Inside sys.tables & sys.views:
ALTER VIEW sys.views AS
SELECT *
FROM sys.objects$
WHERE type = 'V'
ALTER VIEW sys.tables AS
SELECT *
FROM sys.objects$ o
LEFT JOIN sys.sysidxstats lob ON lob.id = o.object_id AND lob.indid <= 1
LEFT JOIN sys.syssingleobjrefs ds ON ds.depid = o.object_id AND ds.class = 8 AND ds.depsubid <= 1 -- SRC_INDEXTOLOBDS
LEFT JOIN sys.syssingleobjrefs rfs ON rfs.depid = o.object_id AND rfs.class = 42 AND rfs.depsubid = 0 -- SRC_OBJTOFSDS
LEFT JOIN sys.syspalvalues ts ON ts.class = 'LEOP' AND ts.value = o.lock_escalation_option
WHERE o.type = 'U'
This will solve your problems
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
You can access all tables and views informations

Resources