2009-11-14 47 views
19

當我右鍵單擊TVP時,我沒有得到像'ALTER TO'這樣的選項如何更改表值參數

+0

ALTER TO for TVP? TVP是功能/過程定義的一部分。不是一個實體本身!? – Nestor 2009-11-14 15:32:51

+0

ALTER TO選項未啓用RT? – anishMarokey 2009-11-14 15:36:17

+3

表值參數是SQL Server 2008中的新參數類型。表值參數是使用用戶定義的表類型聲明的。我想你是問關於ALTERing用戶定義的表類型?據我所知,你可以創建和刪除一個用戶定義的表類型,但不能修改它。 – Nestor 2009-11-14 17:25:36

回答

41

不能這樣做。您必須刪除/重新創建。如果您對TVP的依賴,你必須:

  1. 以新名稱創建新的TVP
  2. ALTER依賴使用(1)
  3. 降舊TVP
  4. 重新創建(1)根據原來的名稱
  5. ALTER依賴使用(4)
  6. 降(1)
+3

謝謝,但麻煩很糟糕。 – 2016-02-18 20:34:38

10

我已經發現了blog post on sqltreeo.com其中有一種方法可以通過臨時刪除依賴項然後重新創建它們來自動執行該過程。

我剛纔修改了一下。

1.You應該創建下列程序:

-- 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.然後,你應該用你的表類型的名稱作爲輸入參數運行它。在網格格式上顯示結果(因爲文本格式可能會截斷長文本),選擇整個結果表並將其複製到新的查詢窗口。

+0

你測試過了嗎? – Shiva 2015-09-07 17:34:35

+0

@Shiva - 是的。它爲我工作。 – BornToCode 2015-09-08 08:00:17

+0

這很酷。節省了我很多時間 – Keith 2016-09-20 14:12:39