Last active
August 29, 2015 14:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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