April 17, 2012

sp_constr_col_usage_rowset2 (Transact-SQL MetaData) Definition

Please note: that the following source code is provided and copyrighted by Microsoft and is for educational purpose only.
The meta data is from an SQL 2012 Server.

I have posted alot more, find the whole list here.

Goto Definition or MetaData

Definition:

sys.sp_constr_col_usage_rowset2(nvarchar @table_schema
, nvarchar @column_name
, nvarchar @constr_catalog
, nvarchar @constr_schema
, nvarchar @constr_name)

MetaData:

   
create procedure sys.sp_constr_col_usage_rowset2
(
@table_schema sysname = null,
@column_name sysname = null,
@constr_catalog sysname = null,
@constr_schema sysname = null,
@constr_name sysname = null
)
as
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
-- copy & pasted from version 1 of the SProc and removed checks for 1st parameter !
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
select *
from
(
select
TABLE_CATALOG = db_name(),
TABLE_SCHEMA = schema_name(obj.schema_id),
TABLE_NAME = obj.name,
COLUMN_NAME = col.name,
COLUMN_GUID = convert(uniqueidentifier, NULL),
COLUMN_PROPID = convert(int, NULL),
CONSTRAINT_CATALOG = db_name(),
CONSTRAINT_SCHEMA = schema_name(obj.schema_id),
CONSTRAINT_NAME = obj2.name

from
sys.all_objects obj inner join
sys.sysconstraints s_sc on (obj.type in ('S ', 'U ') and s_sc.id = obj.object_id and s_sc.colid <> 0) inner join
sys.all_objects obj2 on (s_sc.constid = obj2.object_id),
sys.all_columns col

where
(@table_schema is null or schema_id(@table_schema) = obj.schema_id) and
col.object_id = obj.object_id and
col.column_id = s_sc.colid
) as my_result

where -- ISSUE - PERF - There is no need of 2 nested selects. Incorporate it in the above select.
(@column_name is null or @column_name = my_result.COLUMN_NAME) and
(@constr_catalog is null or @constr_catalog = my_result.CONSTRAINT_CATALOG) and
(@constr_schema is null or @constr_schema = my_result.CONSTRAINT_SCHEMA) and
(@constr_name is null or @constr_name = my_result.CONSTRAINT_NAME)

order by 1, 2, 3, 4, 5, 6, 7, 8, 9

No comments:

Post a Comment

Total Pageviews