日期:2014-05-18 浏览次数:20412 次
USE db1 DECLARE @fieldsList VARCHAR(1000),@curTableName VARCHAR(50),@i INT,@k INT,@newDB VARCHAR(20),@sql VARCHAR(8000) SELECT id=identity(INT),name INTO #tables FROM sysobjects WHERE xtype='u' AND name!='dtproperties' SELECT @i=1,@curTableName='',@newDB='test',@k=COUNT(*) FROM #tables WHILE @i<=@k BEGIN SET @fieldsList='' SELECT @curTableName=name FROM #tables SELECT @fieldsList=@fieldsList + ',' + name FROM syscolumns WHERE id=object_id(@curTableName) AND COLUMNPROPERTY(id,name,'IsIdentity')=0 SELECT @fieldsList=STUFF(@fieldsList,1,1,'') SELECT @sql='IF EXISTS (SELECT 1 FROM ' + @newDb + '..sysobjects WHERE xtype=''u'' AND name=''' + @curTableName + ''') INSERT ' + @newDb + '..' + @curTableName + '(' + @fieldsList + ') SELECT ' + @fieldsList + ' FROM ' + @curTableName + ' ELSE SELECT * INTO ' + @newDb + '..' + @curTableName + ' FROM ' + @curTableName EXEC(@sql) SET @i=@i+1 END DROP TABLE #tables