Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
5Activity
0 of .
Results for:
No results containing your search query
P. 1
sql_FAQ_s1

sql_FAQ_s1

Ratings: (0)|Views: 449|Likes:
Published by api-3736566

More info:

Published by: api-3736566 on Oct 15, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/09/2014

pdf

text

original

SQL Server 2005 Books Online
Querying the SQL Server System Catalog FAQ
Updated: 17 July 2006
This topic contains a list of frequently asked questions. The answers to these questions are queries
that are based on catalog views.
Frequently Asked Questions
The sections below list frequently asked questions by category.
Data Types
\u2022
How do I find the data types of the columns of a specified table?
\u2022
How do I find the LOB data types of a specified table?
\u2022
How do I find the columns that depend on a specified data type?
\u2022
How do I find the computed columns that depend on a specified CLR user-defined type or alias
data type?
\u2022
How do I find the parameters that depend on a specified CLR user-defined type or alias type?
\u2022
How do I find the CHECK constraints that depend on a specified CLR user-defined type?
\u2022
How do I find the views, Transact-SQL functions, and Transact-SQL stored procedures that
depend on a specified CLR user-defined type or alias type?
Tables, Indexes, and Constraints
\u2022
How do I find all the user-defined tables in a specified database?
\u2022
How do I find all the tables that do not have a clustered index in a specified database?
\u2022
How do I find all the tables that do not have an index?
\u2022
How do I find all the tables that do not have a primary key?
\u2022
How do I find all the tables that have an identity column?
\u2022
How do I find all the tables and indexes that are partitioned?
\u2022
How do I find all views in a database?
\u2022
How do I find all the entities that have been modified in the last N days?
\u2022
How do I find the columns of a primary key for a specified table?
\u2022
How do I find the columns of a foreign key for a specified table?
\u2022
How do I determine if a column is used in a computed column expression?
\u2022
How do I find all the columns that are used in a computed column expression?
\u2022
How do I find all the constraints for a specified table?
\u2022
How do I find all the indexes for a specified table?
\u2022
How do I find all the tables that have a specified column name?
\u2022
How do I find all the statistics on a specified object?
\u2022
How do I find all the statistics and statistics columns on a specified object?
Modules (Stored Procedures, User-Defined Functions, and Triggers)
\u2022
How do I find all the stored procedures in a database?
\u2022
How do I find all the user-defined functions in a database?
\u2022
How do I find the parameters for a specified stored procedure or function?
\u2022
How do I find the dependencies on a specified function?
\u2022
How do I view the definition of a module?
\u2022
How do I view the definition of a server-level trigger?
Schemas, Users, Roles, and Permissions
\u2022
How do I find all the owners of entities contained in a specified schema?
\u2022
How do I find the permissions granted or denied to a specified principal?
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<da t aba s e _ name> with a valid database name.

USE <database_name>; GO SELECT SCHEMA_NAME(t.schema_id) AS schema_name, t.name
AS table_name FROM sys.tables AS t WHERE NOT EXISTS ( SELECT * FROM sys.indexes AS i
WHERE i.object_id = t.object_id AND i.type = 1 -- or type_desc = 'CLUSTERED' ) ORDER BY
schema_name, table_name; GO

Or, you can use theOB J EC T PROPERTY function as shown in the following example.

USE <database_name>; GO SELECT SCHEMA_NAME(schema_id) AS schema_name, name AS table_name FROM sys.tables WHERE OBJECTPROPERTY(object_id,'TableHasClustIndex') = 0 ORDER BY schema_id, name; GO

TOP
How do I find all the owners of entities contained in a specified schema?
Before you run the following query, replace<da t aba s e _ name> and<schema _n ame> with
valid names.

USE <database_name>; GO SELECT 'OBJECT' AS entity_type
,USER_NAME(OBJECTPROPERTY(object_id, 'OwnerId')) AS owner_name ,name FROM
sys.objects WHERE SCHEMA_NAME(schema_id) = '<schema_name>' UNION SELECT 'TYPE'

AS entity_type ,USER_NAME(TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name,
'OwnerId')) AS owner_name ,name FROM sys.types WHERE SCHEMA_NAME(schema_id) =
'<schema_name>' UNION SELECT 'XML SCHEMA COLLECTION' AS entity_type
,COALESCE(USER_NAME(xsc.principal_id),USER_NAME(s.principal_id)) AS owner_name
,xsc.name FROM sys.xml_schema_collections AS xsc JOIN sys.schemas AS s ON s.schema_id
= xsc.schema_id WHERE s.name = '<schema_name>'; GO

TOP
How do I find all the tables that do not have a primary key?
Before you run the following queries, replace<da t aba s e _ name> with a valid database name.

USE <database_name>; GO SELECT SCHEMA_NAME(t.schema_id) AS schema_name ,t.name AS table_name FROM sys.tables t WHERE object_id NOT IN ( SELECT parent_object_id FROM sys.key_constraints WHERE type_desc = 'PRIMARY_KEY_CONSTRAINT' -- or type = 'PK' ); GO

Or, you can run the following query.

USE <database_name>; GO SELECT SCHEMA_NAME(schema_id) AS schema_name ,name
AS table_name FROM sys.tables WHERE OBJECTPROPERTY(object_id,'TableHasPrimaryKey') =
0 ORDER BY schema_name, table_name; GO

TOP
How do I find all the tables that do not have an index?
Before you run the following query, replace<da t aba s e _ name> with a valid database name.

USE <database_name>; GO SELECT SCHEMA_NAME(schema_id) AS schema_name ,name AS table_name FROM sys.tables WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0 ORDER BY schema_name, table_name; GO

TOP
How do I find all the tables that have an identity column?
Before you run the following query, replace<da t aba s e _ name> with a valid database name.

USE <database_name>; GO SELECT SCHEMA_NAME(schema_id) AS schema_name , t.name AS table_name , c.name AS column_name FROM sys.tables AS t JOIN sys.identity_columns c ON t.object_id = c.object_id ORDER BY schema_name, table_name; GO

Or, you can run the following query.
Note:
This query does not return the name of the columns.

USE <database_name>; GO SELECT SCHEMA_NAME(schema_id) AS schema_name ,name AS table_name FROM sys.tables WHERE OBJECTPROPERTY(object_id,'TableHasIdentity') = 1 ORDER BY schema_name, table_name; GO

TOP
How do I find the data types of the columns of a specified table?

Activity (5)

You've already reviewed this. Edit your review.
1 hundred reads
rabirm77 liked this
gvgnaidus liked this
Ibrahim Basha liked this
ravipandeyhcl liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->