SQL FAQ S
SQL FAQ S
This topic contains a list of frequently asked questions. The answers to these questions are queries
that are based on catalog views.
Data Types
• How do I find the computed columns that depend on a specified CLR user-defined type or alias
data type?
• How do I find the parameters that depend on a specified CLR user-defined type or alias type?
• How do I find the CHECK constraints that depend on a specified CLR user-defined type?
• How do I find the views, Transact-SQL functions, and Transact-SQL stored procedures that
• How do I find all the tables that do not have a clustered index in a specified database?
• How do I find all the tables that do not have a primary key?
• How do I find all the tables and indexes that are partitioned?
• How do I find all the entities that have been modified in the last N days?
• How do I find all the columns that are used in a computed column expression?
• How do I find all the tables that have a specified column name?
• How do I find all the statistics and statistics columns on a specified object?
Answers
How do I find all the tables that do not have a clustered index in a specified database?
Before you run the following queries, replace <database_name> with a valid database name.
How do I find all the tables that do not have a primary key?
Before you run the following queries, replace <database_name> with a valid database name.
Note:
How do I find all the entities that have been modified in the last N days?
Before you run the following query, replace <database_name> and <n_days> with valid
values.
-- Create a function to return the name of the entity on which the permissions are checked. IF
OBJECT_ID (N'dbo.entity_instance_name', N'FN') IS NOT NULL DROP FUNCTION
dbo.entity_instance_name; GO CREATE FUNCTION dbo.entity_instance_name(@class_desc
nvarchar(60), @major_id int) RETURNS sysname AS BEGIN DECLARE @the_entity_name
sysname SELECT @the_entity_name = CASE WHEN @class_desc = 'DATABASE' THEN
DB_NAME() WHEN @class_desc = 'SCHEMA' THEN SCHEMA_NAME(@major_id) WHEN
@class_desc = 'OBJECT_OR_COLUMN' THEN OBJECT_NAME(@major_id) WHEN @class_desc
= 'DATABASE_PRINCIPAL' THEN USER_NAME(@major_id) WHEN @class_desc = 'ASSEMBLY'
THEN (SELECT name FROM sys.assemblies WHERE assembly_id=@major_id) WHEN
@class_desc = 'TYPE' THEN TYPE_NAME(@major_id) WHEN @class_desc =
'XML_SCHEMA_COLLECTION' THEN (SELECT name FROM sys.xml_schema_collections WHERE
xml_collection_id=@major_id) WHEN @class_desc = 'MESSAGE_TYPE' THEN (SELECT name
FROM sys.service_message_types WHERE message_type_id=@major_id) WHEN @class_desc
= 'SERVICE_CONTRACT' THEN (SELECT name FROM sys.service_contracts WHERE
service_contract_id=@major_id) WHEN @class_desc = 'SERVICE' THEN (SELECT name FROM
sys.services WHERE service_id=@major_id) WHEN @class_desc =
'REMOTE_SERVICE_BINDING' THEN (SELECT name FROM sys.remote_service_bindings
WHERE remote_service_binding_id=@major_id) WHEN @class_desc = 'ROUTE' THEN
(SELECT name FROM sys.routes WHERE route_id=@major_id) WHEN @class_desc =
'FULLTEXT_CATALOG' THEN (SELECT name FROM sys.fulltext_catalogs WHERE
fulltext_catalog_id=@major_id) WHEN @class_desc = 'SYMMETRIC_KEY' THEN (SELECT name
FROM sys.symmetric_keys WHERE symmetric_key_id=@major_id) WHEN @class_desc =
'CERTIFICATE' THEN (SELECT name FROM sys.certificates WHERE certificate_id=@major_id)
WHEN @class_desc = 'ASYMMETRIC_KEY' THEN (SELECT name FROM sys.asymmetric_keys
WHERE asymmetric_key_id=@major_id) WHEN @class_desc = 'SERVER' THEN (SELECT
name FROM sys.servers WHERE server_id=@major_id) WHEN @class_desc =
'SERVER_PRINCIPAL' THEN SUSER_NAME(@major_id) WHEN @class_desc = 'ENDPOINT' THEN
(SELECT name FROM sys.endpoints WHERE endpoint_id=@major_id) ELSE '?' END RETURN
@the_entity_name END; GO -- Return server-level permissions for the user. SELECT class
,class_desc ,dbo.entity_instance_name(class_desc, major_id) AS entity_name ,minor_id
,SUSER_NAME(grantee_principal_id) AS grantee ,SUSER_NAME(grantor_principal_id) AS
grantor ,type ,permission_name ,state_desc FROM sys.server_permissions WHERE
grantee_principal_id = SUSER_ID('public'); GO -- Return database-level permissions for the
user. SELECT class ,class_desc ,dbo.entity_instance_name(class_desc , major_id) AS
entity_name ,minor_id ,USER_NAME(grantee_principal_id) AS grantee
,USER_NAME(grantor_principal_id) AS grantor ,type ,permission_name ,state_desc FROM
sys.database_permissions WHERE grantee_principal_id = DATABASE_PRINCIPAL_ID('public');
GO
TOP
How do I find all the columns that are used in a computed column expression?
Before you run the following query, replace <database_name> with a valid name.
How do I find the columns that depend on a specified CLR user-defined type or alias type?
Before you run the following query, replace <database_name> with a valid name and
How do I find the computed columns that depend on a specified CLR user-defined type or
alias type?
Before you run the following query, replace <database_name> with a valid name and
How do I find the parameters that depend on a specified CLR user-defined type or alias
type?
Before you run the following query, replace <database_name> with a valid name and
How do I find the CHECK constraints that depend on a specified CLR user-defined type?
Before you run the following query, replace <database_name> with a valid name and
<schema_name.da ta_ type_name> with a valid, schema-qualified CLR user-defined type, alias
type name.
The parameters defined in a function or procedure are implicitly schema bound. Therefore,
parameters that depend on a CLR user-defined type or alias type can be viewed by using the
sys.sql_dependencies catalog view. Procedures and triggers are not schema bound. This means that
dependencies between any expression defined in the body of the procedure or trigger and a CLR
user-defined type or alias type is not maintained. Schema bound views and schema bound user-
defined functions that have expressions that depend on a CLR user-defined type or alias type are
maintained in the sys.sql_dependencies catalog view. Dependencies between types and CLR
functions and CLR procedures are not maintained.
The following query returns all schema-bound dependencies in views, Transact-SQL functions, and
Transact-SQL stored procedures for a specified CLR user-defined type or alias type.
How do I find all the objects that have a specified column name?
Before you run the following query, replace <database_name> and <co lumn_name> with
valid names.
How do I find all the tables and indexes that are partitioned?
Before you run the following query, replace <database_name> with a valid name.
<schema_name.ob jec t _name> with a valid table, indexed view, or table-valued function
name.
How do I find all the statistics and statistics columns on a specified object?
Before you run the following query, replace <database_name> with a valid name and
<schema_name.ob jec t _name> with a valid table, indexed view, or table-valued function
name.