Results 1 to 6 of 6

Thread: [RESOLVED] SQL Script Creator

  1. #1

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

    Resolved [RESOLVED] SQL Script Creator

    I have created the following SQL code to create a script to change the owner of a MS SQL Server database:

    SQL Code:
    1. /*Change Owners of a table:*/
    2.  
    3. DECLARE @OldOwner CHAR(25)
    4. DECLARE @NewOwner CHAR(25)
    5.  
    6. SET @OldOwner ='dbo'
    7. SET @NewOwner = 'ADM'
    8.  
    9. SELECT 'exec sp_changeobjectowner ' + CHAR(39) + RTRIM(LTRIM(@OldOwner)) + '.' + _
    10.          Name + CHAR(39) +',' + CHAR(39)+ RTRIM(LTRIM(@NewOwner)) + CHAR(39) Owner_Change_Script
    11. FROM sysobjects
    12. WHERE xtype = 'U' AND UID = 5
    13. ORDER by Name

    This code outputs the following:

    SQL Code:
    1. exec sp_changeobjectowner 'dbo.CLASSES','ADM'
    2. exec sp_changeobjectowner 'dbo.CAUSES','ADM'
    3. exec sp_changeobjectowner 'dbo.LEVEL','ADM'
    4. exec sp_changeobjectowner 'dbo.ACCIDENTS','ADM'
    5. exec sp_changeobjectowner 'dbo.ACTIONS','ADM'


    But I want the Script to insert a 'Go' in between each exec statement like this:

    SQL Code:
    1. exec sp_changeobjectowner 'dbo.CLASSES','ADM'
    2. GO
    3. exec sp_changeobjectowner 'dbo.CAUSES','ADM'
    4. GO
    5. exec sp_changeobjectowner 'dbo.LEVEL','ADM'
    6. GO
    7. exec sp_changeobjectowner 'dbo.ACCIDENTS','ADM'
    8. GO
    9. exec sp_changeobjectowner 'dbo.ACTIONS','ADM'
    10. GO

    But for the life of me I can't seem to figure it out. Any ideas how I can go about doing this??? The databases I have to convert have hundreds and hundreds of tables and when it throws an error I want to be able to click on the error and be brought to the line that threw the error.

    Thanks!
    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
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL Script Creator

    Something like this perhaps:
    Code:
    ... + CHAR(13) + 'GO' + CHAR(13)
    Instead of 13 you may need 10, or 13 followed by 10.

  3. #3

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

    Re: SQL Script Creator

    Quote Originally Posted by si_the_geek
    Something like this perhaps:
    Code:
    ... + CHAR(13) + 'GO' + CHAR(13)
    Instead of 13 you may need 10, or 13 followed by 10.

    Thanks, I tried your suggestion but I get the following:

    SQL Code:
    1. exec sp_changeobjectowner 'dbo.CLASSES','ADM' GO
    2. exec sp_changeobjectowner 'dbo.CAUSES','ADM' GO
    3. exec sp_changeobjectowner 'dbo.LEVEL','ADM' GO

    Which throws an error.

    I probably have to use some sort of UNION but for the life of me I can't get it to work.
    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."


  4. #4

    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."


  5. #5
    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

  6. #6

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

    Re: [RESOLVED] SQL Script Creator

    Nice, thanks Steve!
    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."


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