April 16, 2006 at 2:31 pm
This procedure is useful to put it in scheduled job, then you'll get a fresh data table without making backup and restore.
-------------------------------------------------------------
CREATE PROCEDURE [CopyToNewDatabase]
@Dname varchar (256), --The name of source database
@Sname varchar (256) --The name of source server
as
declare
@Sql varchar (256),
@c cursor,
@Name varchar(256)
SET @C = CURSOR FAST_FORWARD FOR
select [name] from sysobjects where xtype = 'u'
OPEN @C
FETCH NEXT FROM @C INTO @Name
WHILE @@FETCH_STATUS = 0
BEGIN
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'temp1')
DROP TABLE temp1
set @sql = 'select * into temp1'+' '+'from '+@Sname+'.'+@Dname+'.'+'dbo.'+@name
execute(@sql)
set @sql = 'truncate table '+ @name
--print @sql
execute (@Sql)
set @sql = 'alter table '+ @name +' '+ 'disable trigger all'
execute (@sql)
set @sql = 'insert into '+ @name + ' select * from temp1'
--print @sql
execute (@Sql)
set @sql = 'alter table '+ @name +' '+ 'enable trigger all'
execute (@Sql)
--drop table temp1
print @Name
FETCH NEXT FROM @C INTO @Name
end
--EXEC CopyToNewDatabase '',''
April 16, 2006 at 8:33 pm
Nicely done... You should publish this in the "scripts" section of this forum.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 17, 2006 at 8:19 am
Interesting idea. Should work well for small datbases. I'd worry about larger ones.
April 17, 2006 at 11:22 am
Mr Steve
I've tried this script on database with size about 1.5 GB , and it took about 14 minutes.
April 17, 2006 at 3:25 pm
Don't like the idea for several reasons.
Constraints (all kinds) are lost, indexes (all kinds) are lost and the network will be really taxed if the DB grows at "decent" sizes. By the way, a 1.5GB DB is a toy size DB nowdays (no offence)
It is interesting though for small footprint DBs
Cheers,
* Noel
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply