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

  • Home
  • SEARCH
  • 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 7642525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:12:43+00:00 2026-05-31T09:12:43+00:00

I have to insert the output of this code into an already existing table.

  • 0

I have to insert the output of this code into an already existing table. How can I achieve this? I did it, but the output is getting appended to the previous result of the table.

Code:

DECLARE @KPI_BU TABLE (BU NVARCHAR(MAX)) 

INSERT @KPI_BU 
SELECT @BU 

Declare @KTV table(Business_Unit nvarchar(max), KPI nvarchar(max),YTD_Plan money, Actual money, Deviation money, Year_Plan money )

DECLARE BU_cursor CURSOR FAST_FORWARD FOR 
SELECT BU FROM @KPI_BU 

OPEN BU_cursor 
FETCH NEXT FROM BU_cursor INTO @BU 

WHILE @@FETCH_STATUS = 0 
BEGIN 
INSERT @KTV(Business_Unit, KPI, YTD_Plan, Actual, Deviation, Year_Plan) 
EXEC dbo.SP_KPI_History @FiscalYear, @BU 

FETCH NEXT FROM BU_cursor INTO @BU 
END 

CLOSE BU_cursor 
DEALLOCATE BU_cursor 

--SELECT * FROM @KTV

And if I provide

SELECT * 
into KPI_History
FROM @KTV

in place of the commented line it creates a new table and inserts the result into it. I want to insert the RESULT into already existing table.

Update

@Mikael;

I don’t know whether i was able to make u understand my question or not. Sorry for that.

What i want is, Now i have two table with this columns:

KPI_History with(Businessunit,KPI_Name,YTD,Actual,Deviation,..)

KPI_codes with (KPI_Name, KPI_code)

In KPI_history table i want to use the KPI_Code of KPI_Codes table instead of KPI_name of KPI_History table. As KPI_Names are too long strings and i have created short codes for the KPI_Names and valued them into KPI_Code. i.e. KPI_code table consist of the KPI names and the codes for the KPI_Names.

So output should be:
KPI_Table with (Businessunit,KPI_Name[It sholud contain codes from KPI_History table],YTD,etc.)

Hope i was able to make you guys understand what i am asking for. 😐

  • 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-05-31T09:12:45+00:00Added an answer on May 31, 2026 at 9:12 am

    I want to insert the RESULT into already existing table.

    Here is how you insert into an existing table:

    insert into KPI_History (Business_Unit, KPI, YTD_Plan, Actual, Deviation, Year_Plan)
    select Business_Unit, KPI, YTD_Plan, Actual, Deviation, Year_Plan
    from @KTV;
    

    but the output is getting appended to the previous result of the
    table.

    So you don’t want the result to be added to KPI_History? Then I guess you just have to delete the rows in KPI_History before inserting.

    delete from KPI_History;
    
    insert into KPI_History (Business_Unit, KPI, YTD_Plan, Actual, Deviation, Year_Plan)
    select Business_Unit, KPI, YTD_Plan, Actual, Deviation, Year_Plan
    from @KTV;
    

    Update:

    If you need information from another table in the insert you can just join to that table.
    Something like this might work for you.

    insert into KPI_History
      (Business_Unit,
       KPI,
       YTD_Plan,
       Actual,
       Deviation,
       Year_Plan, 
       Code)
    select K.Business_Unit,
           K.KPI, 
           K.YTD_Plan,
           K.Actual,
           K.Deviation,
           K.Year_Plan,
           C.Code
    from @KTV as K
      inner join KPI_Code as C
        on K.KPI = C.KPI;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have this basic mysql code: sql = 'INSERT INTO popularity (PersonNumber, Category,
I have an insert query that gets generated like this INSERT INTO InvoiceDetail (LegacyId,InvoiceId,DetailTypeId,Fee,FeeTax,Investigatorid,SalespersonId,CreateDate,CreatedById,IsChargeBack,Expense,RepoAgentId,PayeeName,ExpensePaymentId,AdjustDetailId)
I have to insert the values in the table named First, but before inserting
So i have this in my database: INSERT INTO `strange` (`id`, `myKey1`, `myValue1`, `myKey2`,
I have this code: $createFields=CREATE TABLE temp_field (fld text NOT NULL, tablename char(50) NOT
I have 2 insert triggers, that fire on same table, they both try to
Hey guys quick question, I currently have an insert statement $query= INSERT into new_mail
Using POSIX threads & C++, I have an Insert operation which can only be
I was inserting multiple arrays into my database using this code: $queries_cols = array();
I don't have much knowledge about regular expression. I found this code to detect

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.