
create table #tables (s varchar(255), t varchar(255))
go 

insert into #tables(s,t)
(select distinct isnull(e.[Schema], ae.[Schema]), substring([MappingName], 1,len([MappingName]) - charindex('.', reverse([MappingName])))
from vclib.T_ENTITY_ATTRIBUTES ea
left join vclib.t_entities e on e.entityid = ea.entityid
left join vclib.T_ADDITIONAL_ENTITIES ae on ae.entityid = ea.entityid
where mappingname like '%.%')


declare @schema varchar(255), @table varchar(255), @command nvarchar(max)
DECLARE @CURSOR CURSOR


SET @CURSOR  = CURSOR
FOR
select  s,t 
from #tables
join ( select table_name as t2 from information_schema.tables where TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA = 'dbo'
		union select v.name as t2 from sys.views v join sys.schemas s on v.schema_id=s.schema_id where s.name = 'dbo') t3 on (cast(t3.t2 as varchar(8000))collate Cyrillic_General_CI_AS) =t
where t<>'T_ENTITIES' and t<>'T_USERS' 

OPEN @CURSOR
FETCH NEXT FROM @CURSOR INTO @schema, @table
WHILE @@FETCH_STATUS = 0
BEGIN

set @command = 'alter schema '+@schema+' transfer dbo.'+@table
exec sp_executesql @command

FETCH NEXT FROM @CURSOR INTO @schema, @table
END
CLOSE @CURSOR


drop table #tables