首页 > 代码库 > 导数据
导数据
从Erp库中把数据导入到 BanChe56 的库。 写一个存储过程。
CREATE proc [dbo].[Dev_MoveTableData]( @ToTable varchar(50), @FromTable varchar(50) , @useId bit , @IDFrom int ,@execute bit ) as begindeclare @sql nvarchar(4000);declare @columns varchar(500);if( @FromTable is null) set @FromTable = @ToTable;set @sql = N‘select * from Erp.dbo.[‘ + @FromTable + ‘]‘ ;exec sp_executesql @sql;set @sql = ‘‘;if( @useId = 1) begin set @sql = N‘set identity_insert BanChe56.dbo.[$ToTable$] oninsert into BanChe56.dbo.[$ToTable$] ($AllColumns$) select $AllColumns$ from Erp.dbo.[$FromTable$] where ID > $IDFrom$set identity_insert BanChe56.dbo.[$ToTable$] off‘ ; select @columns = ‘[‘ + dbo.JoinStr2(name, ‘],[‘) + ‘]‘ from sys.columns where object_id = object_id(@ToTable) and is_computed = 0 ; set @sql = REPLACE(@sql,‘$AllColumns$‘,@columns) ; end else begin set @sql = N‘insert into BanChe56.dbo.[$ToTable$] ($Columns$) select $Columns$ from Erp.dbo.[$FromTable$] where ID > $IDFrom$‘ ; select @columns = ‘[‘ + dbo.JoinStr2(name, ‘],[‘) + ‘]‘ from sys.columns where object_id = object_id(@ToTable) and is_computed = 0 and is_identity = 0; set @sql = REPLACE(@sql,‘$Columns$‘,@columns) ;end set @sql = replace(@sql,‘$FromTable$‘,@FromTable);set @sql = replace(@sql,‘$ToTable$‘,@ToTable);set @sql = replace(@sql,‘$IDFrom$‘, cast( @IDFrom as varchar(50)) ); print @sql ; if( @execute =1 ) begin exec sp_executesql @sql ; end ;end ;GO
检查有数据的表:
SELECT a.name FROM sys.sysobjects AS a
WHERE EXISTS(
SELECT 1 FROM sys.sysindexes AS x
WHERE x.id=a.id AND x.rows>0 )
AND a.type=‘U‘
导数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。