Skip to content

Instantly share code, notes, and snippets.

@philoushka
Last active August 29, 2015 14:22
Show Gist options
  • Save philoushka/b4a02e0a2c34e0e0c37d to your computer and use it in GitHub Desktop.
Save philoushka/b4a02e0a2c34e0e0c37d to your computer and use it in GitHub Desktop.
move/transfer all tables, stored proces, and functions to another SQL Server schema
declare @newSchema varchar(100) = 'newSchema',
@currentSchema varchar(100) = 'dbo';
--tables and views
select 'alter schema ' + @newSchema + ' transfer ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']'
from information_schema.TABLES where TABLE_SCHEMA = @currentSchema
union
--stored procs and functions
select 'alter schema ' + @newSchema + ' transfer ' + ROUTINE_SCHEMA + '.[' + ROUTINE_NAME + ']'
from information_schema.routines
where ROUTINE_SCHEMA = @currentSchema
and left(ROUTINE_NAME, 3) NOT IN ('sp_', 'xp_', 'ms_');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment