Change Schema Name Of Table In SQL

后端 未结 10 2259
清歌不尽
清歌不尽 2020-12-04 06:15

I want to change schema name of table Employees in Database. In the current table Employees database schema name is dbo I want to chan

10条回答
  •  一生所求
    2020-12-04 06:50

    Try below

    declare @sql varchar(8000), @table varchar(1000), @oldschema varchar(1000), @newschema   varchar(1000)
    
      set @oldschema = 'dbo'
      set @newschema = 'exe'
    
     while exists(select * from sys.tables where schema_name(schema_id) = @oldschema)
    
      begin
          select @table = name from sys.tables 
          where object_id in(select min(object_id) from sys.tables where  schema_name(schema_id)  = @oldschema)
    
        set @sql = 'alter schema ' + @newschema + ' transfer ' + @oldschema + '.' + @table
    
       exec(@sql)
     end
    

提交回复
热议问题