Skip to content

Instantly share code, notes, and snippets.

@olivier-spinelli
Last active June 6, 2016 15:09
Show Gist options
  • Save olivier-spinelli/d73124982e7f72c4a81f29675c2c3eb1 to your computer and use it in GitHub Desktop.
Save olivier-spinelli/d73124982e7f72c4a81f29675c2c3eb1 to your computer and use it in GitHub Desktop.
if OBJECT_ID( 'EG.TestWithTable' ) is not null drop procedure EG.TestWithTable;
if TYPE_ID( 'EG.TestWithTableData' ) is not null drop type EG.TestWithTableData;
GO
create type EG.TestWithTableData as table
(
Value int,
Name nvarchar(3),
Doc xml,
Point Geometry,
CName as Name + ' (' + cast(Value as nvarchar) + ')',
primary key (Value,Name)
);
GO
create procedure EG.TestWithTable(
@P1 int,
@P2 EG.TestWithTableData readonly,
@SumP int output
)
as
begin
select @SumP = sum(Value) from @P2;
set @SumP = @SumP * @P1;
return 0;
end
GO
select TypeId = tab.user_type_id,
TypeSchema = s.name,
TypeName = tab.name,
ColName = c.name,
ColType = case when c.is_computed = 1 then null else
case when c.system_type_id != c.user_type_id
then N'[' + schema_name(t.[schema_id]) + N'].[' + t.name + N']'
else t.name
end
+ case
when t.name in (N'varchar', N'char', N'varbinary', N'binary')
then N'(' + case when c.max_length = -1
then N'max'
else cast(c.max_length as nvarchar(5))
end + N')'
when t.name in (N'nvarchar', N'nchar')
then N'(' + case when c.max_length = -1
then N'max'
else cast(c.max_length / 2 as nvarchar(5))
end + N')'
when t.name in (N'datetime2', N'time2', N'datetimeoffset')
then N'(' + cast(c.scale as nvarchar(5)) + N')'
when t.name = N'decimal'
then N'(' + cast(c.[precision] as nvarchar(5)) + N',' + cast(c.scale as nvarchar(5)) + N')'
else N''
end
end,
Collation = case when c.is_computed = 1 or (c.collation_name is null or c.system_type_id != c.user_type_id)
then null
else c.collation_name
end,
Constraints = case when c.is_computed = 1 then N'' else
case when c.is_nullable = 0 then N' not null' else N'' end
+ case when c.default_object_id != 0
then N' constraint [' + object_name(c.default_object_id) + ']' + N' default ' + object_definition(c.default_object_id)
else N''
end
+ case when cc.object_id is not null
then N' constraint [' + cc.name + N'] check ' + cc.[definition]
else N''
end
+ case when c.is_identity = 1
then N' identity(' + cast(identityproperty(c.[object_id], 'SeedValue') as nvarchar(5)) + N',' + cast(identityproperty(c.[object_id], 'IncrementValue') as nvarchar(5)) + N')'
else N''
end
end,
Computed = object_definition(c.object_id, c.column_id)
from sys.table_types tab
inner join sys.schemas s on s.schema_id = tab.schema_id
inner join sys.columns c on c.[object_id] = tab.type_table_object_id
inner join sys.types t with(nolock) on t.user_type_id = c.user_type_id
left outer join sys.check_constraints cc with(nolock) on c.[object_id] = cc.parent_object_id and cc.parent_column_id = c.column_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment