create PROC P_InsDATA_ReturnID
(
@sourcetb VARCHAR(255),
@totb varchar(255),
@sourcedb VARCHAR(128)=NULL,
@todb VARCHAR(128)=NULL,
@sourceschema VARCHAR(128)=NULL,
@toschema VARCHAR(128)=null
)
AS
BEGIN
SET NOCOUNT ON ;
DECLARE @re TINYINT=0,@sobj VARCHAR(500),@tobj VARCHAR(500),@nsql NVARCHAR(4000),
@sourceidcol VARCHAR(128),@toidcol VARCHAR(128),@count INT,@cols NVARCHAR(4000)
IF @sourcedb IS NULL SET @sourcedb=db_name()
IF @todb IS NULL SET @todb=db_name()
IF @sourceschema IS NULL
BEGIN
SET @nsql='select @sourceschema=name from '+QUOTENAME(@sourcedb)+'.sys.schemas
where schema_id = (select schema_id from '+QUOTENAME(@sourcedb)+'.sys.all_objects
where name='''+@sourcetb+''')
'
EXEC sp_executesql @nsql,N'@sourceschema VARCHAR(128) output',@sourceschema OUTPUT
END
IF @toschema IS NULL
BEGIN
SET @nsql='select @toschema=name from '+QUOTENAME(@todb)+'.sys.schemas
where schema_id = (select schema_id from '+QUOTENAME(@todb)+'.sys.all_objects
where name='''+@totb+''')
'
EXEC sp_executesql @nsql,N'@toschema VARCHAR(128) output',@toschema OUTPUT
END
SET @sobj=QUOTENAME(@sourcedb)+'.'+QUOTENAME(@sourceschema)+'.'+QUOTENAME(@sourcetb)
SET @tobj=QUOTENAME(@todb)+'.'+QUOTENAME(@toschema)+'.'+QUOTENAME(@totb)
IF OBJECT_ID(@sobj) IS NULL
BEGIN
RAISERROR('源表%s不存在',11,1,@sobj)
SET @re=1
END
IF OBJECT_ID(@tobj) IS NULL
BEGIN
RAISERROR('目的表%s不存在',11,1,@tobj)
SET @re=1
END
IF @re=1
RETURN
SET @sourceidcol='ident_'+@sourcetb
SET @toidcol='ident_'+@totb
--源自增列处理
PRINT '
源自增列处理:'
SET @nsql='select @count=count(1) from '+QUOTENAME(@sourcedb)+'.sys.all_columns WHERE
(is_identity=1 or name ='''+@sourceidcol+''')
and object_id=object_id('''+@sobj+''')
'
EXEC sp_executesql @nsql ,N'@count int output',@count OUTPUT
IF @count=0
BEGIN
--如果没有自增列,添加
SET @nsql='alter table '+@sobj+' add '+QUOTENAME(@sourceidcol)+' int IDENTITY(1,1) '
EXEC sp_executesql @nsql
PRINT ' 没有自增列,添加OK'
END
ELSE
BEGIN
--否则,获取自增列名称
SET @nsql='select @sourceidcol=name from '+QUOTENAME(@sourcedb)+'.sys.all_columns WHERE is_identity=1
and object_id=object_id('''+@sobj+''') '
EXEC sp_executesql @nsql ,N'@sourceidcol varchar(128) output',@sourceidcol OUTPUT
END
--目的自增列处理
PRINT '
目的自增列处理:'
SET @nsql='select @count=count(1) from '+QUOTENAME(@todb)+'.sys.all_columns WHERE
(is_identity=1 or name ='''+@toidcol+''')
and object_id=object_id('''+@tobj+''')
'
EXEC sp_executesql @nsql ,N'@count int output',@count OUTPUT
IF @count=0
BEGIN
--如果没有自增列,添加
SET @nsql='alter table '+@tobj+' add '+QUOTENAME(@toidcol)+' int IDENTITY(1,1) '
EXEC sp_executesql @nsql
PRINT ' 自增列,添加OK'
END
ELSE
BEGIN
--否则,获取自增列名称
SET @nsql='select @toidcol=name from '+QUOTENAME(@todb)+'.sys.all_columns WHERE is_identity=1
and object_id=object_id('''+@tobj+''') '
EXEC sp_executesql @nsql ,N'@toidcol varchar(128) output',@toidcol OUTPUT
PRINT ' 获取自增列名称OK'
END
--添加目的自增列到源表
PRINT '
添加目的自增列到源表:'
SET @nsql='select @count=count(1) from '+QUOTENAME(@sourcedb)+'.sys.all_columns WHERE name='''+@totb+'_identitycol''
and object_id=object_id('''+@sobj+''')
'
EXEC sp_executesql @nsql ,N'@count int output',@count OUTPUT
PRINT ' 添加目的自增列到源表OK'
IF @count=0
BEGIN
--如果没有目标自增列,添加
SET @nsql='alter table '+@sobj+' add ['+@totb+'_identitycol] int '
EXEC sp_executesql @nsql
PRINT ' 目标自增列,添加OK'
END
--添加源自增列到目的表
PRINT '
添加源自增列到目的表:'
SET @nsql='select @count=count(1) from '+QUOTENAME(@todb)+'.sys.all_columns WHERE name='''+@sourcetb+'_identitycol''
and object_id=object_id('''+@tobj+''')
'
EXEC sp_executesql @nsql ,N'@count int output',@count OUTPUT
PRINT ' 添加源自增列到目的表OK'
IF @count=0
BEGIN
--如果没有源自增列,添加
SET @nsql='alter table '+@tobj+' add ['+@sourcetb+'_identitycol] int '
EXEC sp_executesql @nsql
PRINT ' 没有源自增列,添加OK'
--标记下
SET @re=2
END
--获取相同字段
SET @nsql='
set @cols=''''
select @cols=@cols+'',''+QUOTENAME(name) from (
select name from '+QUOTENAME(@todb)+'.sys.all_columns WHERE object_id=object_id('''+@tobj+''')
intersect
select name from '+QUOTENAME(@sourcedb)+'.sys.all_columns WHERE object_id=object_id('''+@sobj+''')
and name not in ('''+@toidcol+''')
/*目的自增列除外*/
)aa'
EXEC sp_executesql @nsql,N'@cols nvarchar(4000) output',@cols OUTPUT
SET @cols=STUFF(@cols,1,1,'')
--插入数据
PRINT '
插入数据:'
SET @nsql='
insert '+@tobj+'('+
@cols+
(CASE WHEN @sourceidcol=@sourcetb+'_identitycol' THEN '' ELSE ',['+@sourcetb+'_identitycol]' END)+
--(CASE WHEN @toidcol=@totb+'_identitycol' THEN '' ELSE ','+QUOTENAME(@toidcol)+'' END)+
')
select '+
@cols+
(CASE WHEN @sourceidcol=@sourcetb+'_identitycol' THEN '' ELSE ','+QUOTENAME(@sourceidcol) END)+
--(CASE WHEN @toidcol=@totb+'_identitycol' THEN '' ELSE ',['+@totb+'_identitycol]' END)+
' from '+@sobj+'
'
exec sp_executesql @nsql
PRINT ' 插入数据OK'
--反匹数据
PRINT '
反匹数据:'
SET @nsql='
update a
set a.['+@totb+'_identitycol]=b.'+QUOTENAME(@toidcol)+'
from '+@sobj+' a inner join '+@tobj+' b
on a.'+QUOTENAME(@sourceidcol)+'=b.['+@sourcetb+'_identitycol]
'
exec sp_executesql @nsql
PRINT ' 反匹数据OK'
--删除添加到目的表的字段
PRINT '
删除添加到目的表的字段:'
IF @re=2
BEGIN
SET @nsql='alter table '+@tobj+' drop column ['+@sourcetb+'_identitycol] '
exec sp_executesql @nsql
END
PRINT ' 删除添加到目的表的字段OK'
END