Thursday, February 27, 2014

SQL Server change schema to dbo for TABLES and STORED PROCEDURES

Oops... so you mistakenly created some tables , etc, without specifying it belongs to dbo. It's fine, here's a very quick way to remedy it!

SELECT 'ALTER SCHEMA dbo TRANSFER ' + TABLE_SCHEMA + '.' + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'oldschema'

SELECT 'ALTER SCHEMA dbo TRANSFER ' + ROUTINE_SCHEMA + '.' + SPECIFIC_NAME 
FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE='PROCEDURE' AND ROUTINE_SCHEMA = 'oldschema'

Copy and paste the results in Query Analyzer and run.
Related Posts Plugin for WordPress, Blogger...