Am new to Stored procedures.I wrote the stored procedure to copy table from one dtabase to another database.On executing my stored procedures everytime My datas are added in the destination table .My row counts was increasing on every execution.
Please help to resolve the issue.Hope the problem In the loops.
My SP is:
--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
Why are you inserting
crm_next_idas the value inmall_MallID, but using that same id to compare with@mallidto see if the record is already inserted? For example, if you have id5, and you insert a new record with id150, it’s not going to see that the record is already inserted when you run the SP again. Next run, it will add record with id151, then152, and so forth forever. You shouldn’t use the same field as both an auto-increment identity and a foreign key reference at the same time…You either need to use the same
@mallidwhen you insert the new records so they match, or after you generate a new id and insert into the table, update the original recordCMA_UDS.dbo.Dim_Assetto haveAsset_ID=@mallidso they are linked up properly. Which method you use depends on the meanings of those id’s and what constraints you have in your particular application.