Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Change data type on all char(36) columns in db
Message
De
04/09/2013 23:40:38
 
 
À
04/09/2013 23:15:57
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01582234
Message ID:
01582249
Vues:
30
>Yeah, sorry I didn't see that before. To the best of my awareness you need a separate ALTER TABLE statement for each column.
>
>I vaguely recall trying different combinations of syntax before, to avoid separate ALTER TABLE statements, and I was never able to get it work. I think at one point Naomi and I were talking about this, and I don't remember if either of us ever came up with a solution (other than separate ALTER TABLE statements)
>
>I think you can do multiple ADD statements with just one ALTER TABLE, but I don't think you can modify multiple columns with just one ALTER TABLE.

Yep, I just remember this one as being counter intuitive so it bites me (or is that bytes me) every time.

But no big deal - here's the second pass ( cid is the pk in every table so I'll add code for that as well
DECLARE @script TABLE ( scriptstr VARCHAR(MAX) )
DECLARE @scriptstr VARCHAR(MAX)= 'Use InmateTrustfundx' + CHAR(13) + CHAR(10)

DECLARE @guids TABLE
    (
      tbl VARCHAR(100) ,
      tblid INT ,
      col VARCHAR(100) ,
      colid INT
    )

DECLARE @tbl VARCHAR(100)
DECLARE @col VARCHAR(100)
DECLARE @colid INT
DECLARE @tblid INT


INSERT  INTO @guids
        SELECT  tables.name ,
                tables.OBJECT_ID ,
                col.name ,
                col.column_id
        FROM    sys.columns col
                JOIN sys.tables ON col.object_id = tables.OBJECT_ID
        WHERE   system_type_id = 175
                AND max_length = 36
        ORDER BY tables.name ,
                column_id
 
WHILE EXISTS ( SELECT   *
               FROM     @guids ) 
    BEGIN
        SELECT TOP 1
                @tbl = tbl ,
                @col = col ,
                @tblid = tblid ,
                @colid = colid
        FROM    @guids

        SELECT  @scriptstr = @scriptstr + CHAR(13) + CHAR(10) + 'Alter Table '
                + @tbl + CHAR(13) + CHAR(10)
        SELECT  @scriptstr = @scriptstr + CHAR(13) + CHAR(10)
                + 'Alter Column ' + @col + ' Uniqueidentifier ' 
         
        IF @col LIKE 'cid%' 
            BEGIN
                SELECT  @scriptstr = @scriptstr + ' NOT NULL ' + CHAR(13)
                        + CHAR(10)
            END
        ELSE 
            BEGIN
                SELECT  @scriptstr = @scriptstr + '  NULL ' + CHAR(13)
                        + CHAR(10)
            END                      
    
 
        DELETE  FROM @guids
        WHERE   tblid = @tblid
                AND colid = @colid
      
        SELECT  @scriptstr = @scriptstr + 'GO' + CHAR(13) + CHAR(10)
    END
    

RETURN
    
    
Use InmateTrustfundx

Alter Table account_credits

Alter Column cid Uniqueidentifier  NOT NULL 
GO

Alter Table account_credits

Alter Column cinmates_id Uniqueidentifier   NULL 
GO

Alter Table account_debits

Alter Column cpurchases_id Uniqueidentifier   NULL 
GO

Alter Table account_debits

Alter Column cshiftlog_id Uniqueidentifier   NULL 
GO

Alter Table account_debits

Alter Column cbank_checks_id Uniqueidentifier   NULL 
GO
...


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform