Results 1 to 6 of 6

Thread: [RESOLVED] SQL Script Creator

Hybrid View

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: SQL Script Creator

    This is a little overkill but I used a Cursor to output the script the way I wanted it to look:

    SQL Code:
    1. /*Change Owners of a table:*/
    2.  
    3. DECLARE @TargetOwner CHAR(25)
    4. DECLARE @SourceOwner CHAR(25)
    5. DECLARE @OwnerScript CHAR(100)
    6.  
    7. SET @TargetOwner ='dbo'
    8. SET @SourceOwner = 'IA_ADM'
    9.  
    10.  
    11. DECLARE TablesCC CURSOR
    12. FOR SELECT 'exec sp_changeobjectowner ' + CHAR(39) + RTRIM(LTRIM(@TargetOwner)) + '.' + [Name] + CHAR(39) +',' + CHAR(39)+ RTRIM(LTRIM(@SourceOwner)) + CHAR(39) [Owner Change Script]
    13.     FROM sysobjects
    14.     WHERE xtype = 'U' AND UID = 5
    15.     ORDER by [Name]
    16.  
    17. OPEN TablesCC
    18.  
    19. FETCH NEXT FROM TablesCC INTO @OwnerScript
    20.  
    21. WHILE @@FETCH_STATUS=0
    22.     BEGIN
    23.         PRINT @OwnerScript
    24.         PRINT 'GO'
    25.         FETCH NEXT FROM TablesCC INTO @OwnerScript
    26.     END
    27.  
    28.  
    29. Close TablesCC
    30. DEALLOCATE TablesCC
    Last edited by Mark Gambo; Apr 8th, 2008 at 05:43 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] SQL Script Creator

    I love the challenge of making something SET-based - it's great!

    I've done this before so it came quickly

    Code:
    /*Change Owners of a table:*/
     
    Declare @Dup Table (Dup int)
    Insert into @Dup values (1)
    Insert into @Dup values (2)
    
    DECLARE @OldOwner CHAR(25)
    DECLARE @NewOwner CHAR(25) 
    
    SET @OldOwner ='dbo'
    SET @NewOwner = 'ADM' 
    
    SELECT Case When Dup=2 Then 'Go' Else 'exec sp_changeobjectowner ' + CHAR(39) + RTRIM(LTRIM(@OldOwner)) + '.' + 
             Name + CHAR(39) +',' + CHAR(39)+ RTRIM(LTRIM(@NewOwner)) + CHAR(39) End Owner_Change_Script
    FROM sysobjects
    Left Join @Dup on 1=1
    WHERE xtype = 'U' --AND UID = 5
     ORDER BY Name,Dup
    Create a dummy table with two rows. Join to that table with a "forced true".

    You end up doubling your rows.

    And each row has a unique identifier from the dummy table.

    You end up getting:

    Code:
    Owner_Change_Script
    ------------------------------------------------
    exec sp_changeobjectowner 'dbo.Addr_T','ADM'
    Go
    exec sp_changeobjectowner 'dbo.AppConnect_T','ADM'
    Go
    exec sp_changeobjectowner 'dbo.AppElem_T','ADM'
    Go
    .
    .
    .
    Last edited by szlamany; Apr 9th, 2008 at 05:47 AM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width