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 22:56:33
 
 
À
04/09/2013 22:25:54
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:
01582245
Vues:
27
> I normally hate the idea of procedural thinking in tsql but this is easy to write and easy to tweak. Once I have the script I can pretty much solder it.
>
>(did I mention I'm REALLY glad you're back <g> )

>
>Yup, it sounds like you've got it.
>
>And thanks, I really appreciate it.

Here's the results :
DECLARE @scriptstr VARCHAR(MAX)= 'Use InmateTrustfundx' + CHAR(13) + CHAR(10)

DECLARE @guids TABLE
    (
      tbl VARCHAR(100) ,
      col VARCHAR(100)
    )

DECLARE @tables TABLE ( tbl VARCHAR(100) )
DECLARE @cols TABLE ( col VARCHAR(100) )
DECLARE @tbl VARCHAR(100)
DECLARE @col VARCHAR(100)

INSERT  INTO @guids
        SELECT  tables.name ,
                col.name 
        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
 
SELECT  *
FROM    @guids
 
INSERT  INTO @tables
        SELECT DISTINCT
                tbl
        FROM    @guids

SELECT  *
FROM    @tables

WHILE EXISTS ( SELECT   *
               FROM     @tables ) 
    BEGIN
        SELECT TOP 1
                @tbl = tbl
        FROM    @tables

        SELECT  @scriptstr = @scriptstr + CHAR(13) + CHAR(10) + 'Alter Table '
                + @tbl + CHAR(13) + CHAR(10)
    
        DELETE  FROM @cols
        INSERT  INTO @cols
                SELECT  col
                FROM    @guids
                WHERE   tbl LIKE @tbl + '%'
    
        WHILE EXISTS ( SELECT   *
                       FROM     @cols ) 
            BEGIN
                SELECT TOP 1
                        @col = col
                FROM    @cols
	
                SELECT  @scriptstr = @scriptstr + 'Alter column ' + @col
                        + ' uniqueidentifier' + CHAR(13) + CHAR(10)
                DELETE  FROM @cols
                WHERE   col LIKE @col + '%'
                
            END	
            
        DELETE  FROM @tables
        WHERE   tbl LIKE @tbl
        
    END
RETURN
    
Output on the small test db (will probably stick in a go between each alter table and maybe keep the object_id of the table so I don't need the LIKE stuff, but this is definitely proof of concept)
Use InmateTrustfundx

Alter Table account_credits
Alter column cid uniqueidentifier
Alter column cinmates_id uniqueidentifier

Alter Table account_debits
Alter column cpurchases_id uniqueidentifier
Alter column cshiftlog_id uniqueidentifier
Alter column cbank_checks_id uniqueidentifier
Alter column cpayout_requests_id uniqueidentifier
Alter column cexports_id uniqueidentifier
Alter column ctransfer_batches_id uniqueidentifier
Alter column cycreditedamt_exports_id uniqueidentifier
Alter column cycreditedamt_shiftlogid uniqueidentifier
Alter column cbookings_id uniqueidentifier
Alter column cacct_events_id uniqueidentifier


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