IF EXISTS (
SELECT
1
FROM
sys.types t
join
sys.schemas s
on
t.schema_id=s.schema_id
and
t.
name
=
'MyTableType'
and
s.
name
=
'dbo'
)
EXEC
sys.sp_rename
'dbo.MyTableType'
,
'obsoleting_MyTableType'
;
GO
--重建TYPE,比如原来是四个字段,现在想修改为三个字段,或者原来有三个字段想加一个字段变成四个字段
CREATE
TYPE dbo.MyTableType
AS
TABLE
(
Id
INT
NOT
NULL
,
Name
VARCHAR
(255)
NOT
NULL
, Remark
VARCHAR
(255)
)
GO
--将原来引用将要删除的TYPE全部重建一遍,否则原始存储过程会报错
DECLARE
@
Name
NVARCHAR(500);
DECLARE
REF_CURSOR
CURSOR
FOR
SELECT
referencing_schema_name +
'.'
+ referencing_entity_name
FROM
sys.dm_sql_referencing_entities(
'dbo.MyTableType'
,
'TYPE'
);
OPEN
REF_CURSOR;
FETCH
NEXT
FROM
REF_CURSOR
INTO
@
Name
;
WHILE (@@FETCH_STATUS = 0)
BEGIN
EXEC
sys.sp_refreshsqlmodule @
name
= @
Name
;
FETCH
NEXT
FROM
REF_CURSOR
INTO
@
Name
;
END
;
CLOSE
REF_CURSOR;
DEALLOCATE
REF_CURSOR;
GO
--最后删除原始的被重命名的TableType(被第一步重名的那个)
IF EXISTS (
SELECT
1
FROM
sys.types t
join
sys.schemas s
on
t.schema_id=s.schema_id
and
t.
name
=
'obsoleting_MyTableType'
and
s.
name
=
'dbo'
)
DROP
TYPE dbo.obsoleting_MyTableType
GO
--最后执行授权
GRANT
EXECUTE
ON
TYPE::dbo.MyTableType
TO
public
GO