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

SET @CURSOR  = CURSOR
FOR
SELECT distinct [Schema]
  FROM  T_ENTITIES

OPEN @CURSOR
FETCH NEXT FROM @CURSOR INTO @schema
WHILE @@FETCH_STATUS = 0
BEGIN
	IF (NOT EXISTS (SELECT * FROM sys.schemas WHERE name = @schema)) 
	BEGIN
		set @command = 'create schema '+@schema
		exec sp_executesql @command
	END
FETCH NEXT FROM @CURSOR INTO @schema
END
CLOSE @CURSOR


IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[T_ENTITIES_FillBaseEntity]'))
DROP TRIGGER [dbo].[T_ENTITIES_FillBaseEntity]
GO

alter schema VCLib transfer dbo.T_ENTITIES
