The code is this: And I want to create the tables in the destiny database only if the tables doesn't exist. If the tables exists in the destiny database it give's me a PRINT message 'The table database already exist', and only copy the databases that doesn't exist. HELP ME PLEASE!!! I HAVE TO DELIVERY THIS WORK TO THE END OF THE DAY!! PLEASE!
CODE:
create proc CriaTablesBD @DBDest varchar(30)
as begin
declare @CreateTable varchar(255)
declare Tabelas cursor for
select Name , ID
from sysobjects
where Name not like 'sys%'
and type ='U'
Declare @name varchar(255), @ID int
--variaveis usadas para as colunas de uma tabela
declare @NameCol varchar(50),
@lengthCol int,
@typeCol varchar(255),
open Tabelas
fetch Tabelas into @Name , @ID
while (@@FETCH_STATUS = 0)
begin
--** codigo ** --
--select @Name as Nome
--select @ID as ID
declare cursorColun cursor for
select c.Name , c.length , t.name
from syscolumns c , systypes t
where c.xtype=t.xtype
and ID=@ID
select @CreateTable='create table ' +@DBDest + '.dbo.' + @Name + '( '
open cursorColun
fetch cursorColun into @NameCol ,@lengthCol ,@typeCol
while (@@FETCH_STATUS = 0)
begin
select @CreateTable = @CreateTable + @NameCol + ' '+ @typecol + ' '
if (@typecol = 'varchar') begin
select @CreateTable = @CreateTable + '( '+ convert(varchar(10),@lengthCol) + ') '
end
select @CreateTable =@CreateTable + ' , '
fetch cursorColun into @NameCol ,@lengthCol ,@typeCol
end
close cursorColun
DEALLOCATE cursorColun
--**fim codigo **--
fetch Tabelas into @Name , @ID
select @CreateTable =substring (@CreateTable , 1, len(@CreateTable)- 1 )
select @CreateTable =@CreateTable + ')'
select @CreateTable
exec (@CreateTable )
end
close Tabelas
DEALLOCATE Tabelas
end
/*
CriaTablesBD 'DillyHistorico'
*/