日期:2014-05-17 浏览次数:20752 次
USE A /*在A库创建存储过程*/
CREATE PROC SP_IMPORTDATA
AS
BEGIN
DECLARE @i INT,
@iCount INT,
@Columns varchar(3000),
@ExecStr VARCHAR(8000),
@TableName VARCHAR(40)
/*将TSQL201102 这一类的表查询出来插入到临时表*/
if exists(select * from tempdb.dbo.sysobjects where id = object_id(N'[tempdb].[dbo].[#tmp_TableList]'))
begin
drop table [dbo].[#tmp_TableList]
end
select distinct ID = IDENTITY(int,1,1),[NAME] AS TableName
into #tmp_TableList
FROM dbo.SysObjects
WHERE xtype = 'U'
AND [NAME] LIKE 'TSQL201102%'
SET @i = 1
SELECT @iCount = COUNT(1) FROM #tmp_TableList
IF @iCount < 1
BEGIN
RETURN -2
END
/*然后循环临时 将每个表的数据插入到B库对应的表*/
WHILE @i < @iCount+1
BEGIN
SELECT @TableName = TableName FROM #tmp_TableList
WHERE ID = @i
SET @Columns = ''
SET @ExecStr = ''
EXEC('if exists(select * from B.dbo.SysObjects where NAME = '''+@TableName+''' AND xtype = ''U'')
begin
drop table B.dbo.'+@TableName+'
end ')
EXEC('SELECT TOP 0 * INTO [B].[DBO].'+@TableName+' FROM '+@TableName)
/*这里是动态取表字段,自动增长的列和timestamp类型的数据不参与插入.
timestamp 这种数据类型表现自动生成的二进制数,确保这些数在数据库中是唯一的。
timestamp 一般用作给表行加版本戳的机制。存储大小为 8 字节*/
SELECT @Columns = @Columns + NAME + ',' FROM syscolumns A