当我右键点击TVP时,我没有得到'ALTER TO'这样的选项
答案 0 :(得分:44)
不能这样做。您必须删除/重新创建。如果您对TVP有依赖性,您必须:
答案 1 :(得分:13)
我找到了一个blog post on sqltreeo.com,它可以通过临时删除依赖项然后重新创建它们来自动化该过程。
我刚修改了一下。
1.您应该创建以下程序:
-- Find all referencing objects to user-defined table type in @fullObjectName parameter
-- and generate DROP scripts and CREATE scripts for them
CREATE PROC [dbo].[alterTableType] (@fullObjectName VARCHAR(200))
AS
BEGIN
SET NOCOUNT ON
IF (TYPE_ID (@fullObjectName) IS NULL)
BEGIN
RAISERROR ('User-defined table type ''%s'' does not exists. Include full object name with schema.', 16,1, @fullObjectName)
RETURN
END;
WITH sources
AS
(
SELECT ROW_NUMBER() OVER (ORDER BY OBJECT_NAME(m.object_id)) RowId, definition
FROM sys.sql_expression_dependencies d
JOIN sys.sql_modules m ON m.object_id = d.referencing_id
JOIN sys.objects o ON o.object_id = m.object_id
WHERE referenced_id = TYPE_ID(@fullObjectName)
)
SELECT 'BEGIN TRANSACTION'
UNION ALL
SELECT
'DROP ' +
CASE OBJECTPROPERTY(referencing_id, 'IsProcedure')
WHEN 1 THEN 'PROC '
ELSE
CASE
WHEN OBJECTPROPERTY(referencing_id, 'IsScalarFunction') = 1 OR OBJECTPROPERTY(referencing_id, 'IsTableFunction') = 1 OR OBJECTPROPERTY(referencing_id, 'IsInlineFunction') = 1 THEN 'FUNCTION '
ELSE ''
END
END
+ SCHEMA_NAME(o.schema_id) + '.' +
+ OBJECT_NAME(m.object_id)
FROM sys.sql_expression_dependencies d
JOIN sys.sql_modules m ON m.object_id = d.referencing_id
JOIN sys.objects o ON o.object_id = m.object_id
WHERE referenced_id = TYPE_ID(@fullObjectName)
UNION ALL
SELECT 'GO'
UNION ALL
SELECT CHAR(13) + CHAR(10) + '---- WRITE HERE SCRIPT TO DROP OLD USER DEFINED TABLE TYPE AND CREATE A NEW ONE ----' + CHAR(13) + CHAR(10)
UNION ALL
SELECT
CASE
WHEN number = RowId THEN DEFINITION
ELSE 'GO'
END
FROM sources s
JOIN (SELECT DISTINCT number FROM master.dbo.spt_values) n ON n.number BETWEEN RowId AND RowId+1
UNION ALL
SELECT 'COMMIT'
END
2.然后您应该使用您的表类型名称作为输入参数来运行它。以网格格式显示结果(因为文本格式可能会截断长文本),选择整个结果表并将其复制到新的查询窗口。
答案 2 :(得分:0)
只要其他定义引用了用户定义表类型,您就不能删除它:
无法删除类型' dbo.MyTableType ',因为它已被对象' MyStoredProcedure '引用。可能还有其他引用此类型的对象。
如果SSMS为您提供所有其他对象的列表,那就太好了,但是如果您没有很多其他对象,则可以使用部分手动的方法。
要获取使用您的TVP类型的所有SP的列表,可以查询sys.sql_expression_dependencies
SELECT OBJECT_NAME(d.referencing_id)
FROM sys.sql_expression_dependencies d
WHERE d.referenced_id = TYPE_ID('MyTableType')
DROP and CREATE to
新窗口DROP PROCEDURE
部分DROP and CREATE to
新窗口并进行任何更改CREATE
部分答案 3 :(得分:0)
您将取回使用相同TVP的TVP和所有Sp。