Tuesday, June 13, 2017

Sript for table schema changes



Declare @tablename as VarChar (128)
Declare @NewOwner as VarChar(15)
Declare @NewObjOwner as VarChar(8)
Set @NewObjOwner = 'NewSchema'
--Set @NewOwner = @NewObjOwner + 'XX'
DECLARE tables CURSOR FOR
   SELECT TABLE_NAME
   FROM INFORMATION_SCHEMA.TABLES
   WHERE (TABLE_TYPE = 'BASE TABLE' OR TABLE_TYPE = 'VIEW') AND TABLE_SCHEMA = 'OldSchema'
FOR READ ONLY
OPEN tables
FETCH NEXT
   FROM tables
   INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN

   Set @tablename = 'OldSchema.' + @tablename
   EXEC sp_changeobjectowner @tablename , @NewOwner
   FETCH NEXT
      FROM tables
      INTO @tablename
END
CLOSE tables
DEALLOCATE tables

No comments:

Post a Comment