Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:35:01+00:00 2026-06-10T12:35:01+00:00

Am new to Stored procedures.I wrote the stored procedure to copy table from one

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:35:02+00:00Added an answer on June 10, 2026 at 12:35 pm

    Why are you inserting crm_next_id as the value in mall_MallID, but using that same id to compare with @mallid to see if the record is already inserted? For example, if you have id 5, and you insert a new record with id 150, 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 id 151, then 152, 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 @mallid when you insert the new records so they match, or after you generate a new id and insert into the table, update the original record CMA_UDS.dbo.Dim_Asset to have Asset_ID = @mallid so 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to writing Stored Procedure. So I wrote one with output parameters
I am wrote stored procedure to Insert data into database table but i am
I am new stored procedures and trying to write a procedure to duplicate user(by
I am trying to modify a stored procedure I wrote to pull data from
I wrote some stored procedure to transfer 2 tables data to a new, merged
I would like to create a template for new stored procedures for our development
I'm new to stored procedure world. We have existing system with existing stored procedure,
I added a new field to a stored procedure. How do I get it
I'm very new to MySQL and I have a stored procedure that I'd like
I am building a new web applications with data stored in the database. as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.