多個表的數據轉移到另一個表中的存儲過程

use TMS_DEV
if exists(select * from sysobjects where id=OBJECT_ID(N'USP_DataLoad_SubClient')
and OBJECTPROPERTY(id,N'IsProcedure')=1)
drop procedure USP_DataLoad_SubClient
go

create procedure USP_DataLoad_SubClient
@User int
as
declare @SourceSystemId int
select @SourceSystemId=SourceSystemId from T_CSC_SourceSystem where SourceSystemName='Hyperion'
begin

	update T_CSC_SubClient set
	UpdatedAt=getutcdate(),
	UpdatedBy=@User,
	SubClientName=TTBC.SUBCLIENT_NAME
	from(
	select SUBCLIENT_CODE,SUBCLIENT_NAME from T_TMS_Batch_Client
	) as TTBC where T_CSC_SubClient.SubClientCode=TTBC.SUBCLIENT_CODE and SourceSystemId=@SourceSystemId

	
	insert into T_CSC_SubClient(
		ClientId,
		SourceSystemId,
		SubClientCode,
		SubClientName,
		CreatedAt,
		CreatedBy,
		UpdatedAt,
		UpdatedBy
	)select
		null,
		@SourceSystemId,
		TTBC.SUBCLIENT_CODE,
		TTBC.SUBCLIENT_NAME,
		getutcdate(),
		@User,
		getutcdate(),
		@User
		from T_TMS_Batch_Client TTBC where TTBC.SUBCLIENT_CODE not in
		(select distinct SubClientCode from T_CSC_SubClient a where a.SourceSystemId=@SourceSystemId)
end
go

這裏其中經過取其中一個表的一列或者幾列,不用遊標,純屬的原生sql語句。sql

相關文章
相關標籤/搜索