Results 1 to 6 of 6

Thread: [RESOLVED] SQL Script Creator

Threaded View

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


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