How to change schema of all tables, views and stored procedures in MSSQL

后端 未结 4 1750
清歌不尽
清歌不尽 2020-12-13 00:37

Recently we were having issues on our database server and after long efforts it was decided to change the database server. So we managed to restore the database on another s

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 01:18

    Thanks for the tip.. Here is my update to same, where I added a crlf to output as well as put brackets around the SchemaName and ObjectName, because some of the objects had a '-' in the name and the brackets solved that naming error.

    SELECT 'ALTER SCHEMA NewSchemaName TRANSFER [' + SysSchemas.Name + '].[' +      DbObjects.Name + '];'
    + CHAR(13)+ CHAR(10)+ 'GO '+ CHAR(13)+ CHAR(10)
    FROM sys.Objects DbObjects
    INNER JOIN sys.Schemas SysSchemas ON DbObjects.schema_id =     SysSchemas.schema_id
    WHERE SysSchemas.Name = 'OldSchemaName'
    AND (DbObjects.Type IN ('U', 'P', 'V'))
    

提交回复
热议问题