1
我是存儲過程的新手。我編寫存儲過程以將表從一個dtabase複製到另一個數據庫。每次執行我的存儲過程時我的數據被添加到目標表中。我的行數每次執行都在增加。數據每次都在追加運行存儲過程
請幫助解決問題。在循環中尋找問題。
我的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 @[email protected]
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
[email protected],mall_Name = @mallname,[email protected],mall_uds_UOMID [email protected],[email protected],
[email protected],[email protected],[email protected],mall_UpdatedBy=1,
mall_UpdatedDate=GETDATE(),[email protected],mall_AMOSUploaded=GETDATE()
where
[email protected]
end
FETCH NEXT FROM cur_address INTO @mallid,@mallname,@mallstatus,@malludsuomid,@malludsassetcode,@malludsassettype,@malludsremarks,@malludsdwdb--,@mallsecterr
end
CLOSE cur_address
DEALLOCATE cur_address
End