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. 😐
Here is how you insert into an existing table:
So you don’t want the result to be added to
KPI_History? Then I guess you just have to delete the rows inKPI_Historybefore inserting.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.