我是存储过程的新手。我编写了存储过程以将表从一个数据库复制到另一个数据库。每次执行我的存储过程时,我的数据被添加到目标表中。每次执行时我的行数都在增加。
请帮助解决问题。希望问题在循环中。
我的SP是:
--exec mall
alter procedure mall
as
begin
declare @mallid int
declare @mallname nvarchar(40)
declare @mallstatus nvarchar(40)
declare @malludsuomid nchar(2)
declare @malludsassetcode nvarchar(6)
declare @malludsassettype nvarchar(15)
declare @malludsremarks nvarchar(max)
declare @malludsdwdb int
declare @mallsecterr int
declare @mallassetid int
declare @secterr int
declare @Maxmallid int
declare @mallentityid int
Select @mallentityid = customtable.Bord_TableId From CRM.dbo.Custom_Tables as customtable With (NoLock) Where Upper(Bord_Caption) = Upper('Mall') And Bord_Deleted Is Null
DECLARE cur_address CURSOR FOR
SELECT
udsasset.Asset_ID,udsasset.Asset_Name,udsasset.Asset_Status,udsasset.UOM_ID, udsasset.Asset_Code,udsasset.Asset_Type,udsasset.Remarks,udsasset.DW_Key_Source_DB --,crmterr.TPro_SecTerr
from
CMA_UDS.dbo.Dim_Asset as udsasset
OPEN cur_address
FETCH NEXT FROM cur_address INTO @mallid,@mallname,@mallstatus,@malludsuomid,@malludsassetcode,@malludsassettype,@malludsremarks,@malludsdwdb --,@mallsecterr
WHILE @@FETCH_STATUS = 0
BEGIN
if not exists (select crmmall.mall_MallID from CRM.dbo.Mall as crmmall where crmmall.mall_MallID = @mallid)
begin
exec @Maxmallid = CRM.dbo.crm_next_id @Table_Id=@mallentityid
insert into
CRM.dbo.Mall
(mall_MallID,mall_Name,mall_Status,mall_uds_UOMID,mall_uds_asset_code,mall_uds_asset_type,
mall_uds_remarks,mall_uds_dw_db,mall_CreatedBy,mall_CreatedDate,mall_Secterr,mall_AMOSUploaded,mall_asset_id)
values(@Maxmallid,@mallname,@mallstatus,@malludsuomid,@malludsassetcode,@malludsassettype,@malludsremarks,@malludsdwdb,1,GETDATE(),
@mallsecterr,GETDATE(),@mallid)
end
else
begin
update
CRM.dbo.Mall
set
mall_asset_id=@mallid,mall_Name = @mallname,mall_Status=@mallstatus,mall_uds_UOMID =@malludsuomid,mall_uds_asset_code=@malludsassetcode,
mall_uds_asset_type=@malludsassettype,mall_uds_remarks=@malludsremarks,mall_uds_dw_db=@malludsdwdb,mall_UpdatedBy=1,
mall_UpdatedDate=GETDATE(),mall_Secterr=@mallsecterr,mall_AMOSUploaded=GETDATE()
where
mall_MallID=@mallid
end
FETCH NEXT FROM cur_address INTO @mallid,@mallname,@mallstatus,@malludsuomid,@malludsassetcode,@malludsassettype,@malludsremarks,@malludsdwdb--,@mallsecterr
end
CLOSE cur_address
DEALLOCATE cur_address
End