Alter user defined type in SQL Server

后端 未结 10 1130
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 19:47

I created few user defined types in my database as below

CREATE TYPE [dbo].[StringID] FROM [nvarchar](20) NOT NULL

and assigned them to vario

10条回答
  •  鱼传尺愫
    2020-12-02 20:19

    there's a good example of a more comprehensive script here

    It's worth noting that this script will include views if you have any. I ran it and instead of exec'ing inline generated a script as the output which I then tweaked and ran.

    Also, if you have functions/sprocs using the user defeined types you'll need to drop those before running your script.

    Lesson Learned: in future, don't bother with UDTs they're more hassle than they're worth.

    SET NOCOUNT ON
    
    DECLARE @udt VARCHAR(150)
    DECLARE @udtschema VARCHAR(150)
    DECLARE @newudtschema VARCHAR(150)
    DECLARE @newudtDataType VARCHAR(150)
    DECLARE @newudtDataSize smallint
    DECLARE @OtherParameter VARCHAR(50)
    
    SET @udt = 'Name' -- Existing UDDT
    SET @udtschema = 'dbo' -- Schema of the UDDT
    SET @newudtDataType = 'varchar' -- Data type for te new UDDT
    SET @newudtDataSize = 500 -- Lenght of the new UDDT
    SET @newudtschema = 'dbo' -- Schema of the new UDDT
    SET @OtherParameter = ' NULL' -- Other parameters like NULL , NOT NULL
    DECLARE @Datatype VARCHAR(50),
        @Datasize SMALLINT
    
    DECLARE @varcharDataType VARCHAR(50)
    
    DECLARE @Schemaname VARCHAR(50),
        @TableName VARCHAR(50),
        @FiledName VARCHAR(50)
    
    CREATE TABLE #udtflds
        (
          Schemaname VARCHAR(50),
          TableName VARCHAR(50),
          FiledName VARCHAR(50)
        )
    
    SELECT TOP 1
            @Datatype = Data_type,
            @Datasize = character_maximum_length
    FROM    INFORMATION_SCHEMA.COLUMNS
    WHERE   Domain_name = @udt
            AND Domain_schema = @udtschema
    
    SET @varcharDataType = @Datatype
    IF @DataType Like '%char%'
        AND @Datasize IS NOT NULL
        AND ( @newudtDataType <> 'varchar(max)'
              OR @newudtDataType <> 'nvarchar(max)'
            )
        BEGIN
            SET @varcharDataType = @varcharDataType + '('
                + CAST(@Datasize AS VARCHAR(50)) + ')'
        END
    
    INSERT  INTO #udtflds
            SELECT  TABLE_SCHEMA,
                    TABLE_NAME,
                    Column_Name
            FROM    INFORMATION_SCHEMA.COLUMNS
            WHERE   Domain_name = @udt
                    AND Domain_schema = @udtschema
    
    DECLARE @exec VARCHAR(500)
    
    DECLARE alter_cursor CURSOR
        FOR SELECT  Schemaname,
                    TableName,
                    FiledName
            FROM    #udtflds
    
    OPEN alter_cursor
    FETCH NEXT FROM alter_cursor INTO @Schemaname, @TableName, @FiledName
    
    WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @exec = 'Alter Table ' + @Schemaname + '.' + @TableName
                + '  ALTER COLUMN ' + @FiledName + ' ' + @varcharDataType
            EXECUTE ( @exec
                   )
            FETCH NEXT FROM alter_cursor INTO @Schemaname, @TableName, @FiledName
    
        END
    
    CLOSE alter_cursor
    
    SET @exec = 'DROP TYPE [' + @udtschema + '].[' + @udt + ']'
    EXEC ( @exec
        )
    
    SET @varcharDataType = @newudtDataType
    
    IF @newudtDataType Like '%char%'
        AND @newudtDataSize IS NOT NULL
        AND ( @newudtDataType <> 'varchar(max)'
              OR @newudtDataType <> 'nvarchar(max)'
            )
        BEGIN
            SET @varcharDataType = @varcharDataType + '('
                + CAST(@newudtDataSize AS VARCHAR(50)) + ')'
        END
    
    SET @exec = 'CREATE TYPE [' + @newudtschema + '].[' + @udt + '] FROM '
        + @varcharDataType + ' ' + @OtherParameter
    EXEC ( @exec
        )
    
    OPEN alter_cursor
    FETCH NEXT FROM alter_cursor INTO @Schemaname, @TableName, @FiledName
    
    WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @exec = 'Alter Table ' + @Schemaname + '.' + @TableName
                + '  ALTER COLUMN ' + @FiledName + ' ' + '[' + @newudtschema
                + '].[' + @udt + ']'
            EXECUTE ( @exec
                   )
            FETCH NEXT FROM alter_cursor INTO @Schemaname, @TableName, @FiledName
        END
    
    CLOSE alter_cursor
    DEALLOCATE alter_cursor
    SELECT  *
    FROM    #udtflds
    
    DROP TABLE #udtflds
    

    1: http://www.sql-server-performance.com/2008/how-to-alter-a-uddt/ has replaced http://www.sql-server-performance.com/faq/How_to_alter_a%20_UDDT_p1.aspx

提交回复
热议问题