Bubai Banerjee

Bubai Banerjee

  • 1.6k
  • 163
  • 45.8k

How to insert data from one table to another table after cal

Jan 19 2015 8:22 AM
-- I have a table "myStoreTable".which contain 4 columns.
-- I a have another table "tblFinalTable", which contain 5 columns.
-- I want to insert data into "tblFinalTable" from "tblmyStoreTable".
-- But, not directly inserted,after calculating the "EmpInterest", it inserted into tblFinalTable table.
-- But i am facing problem while try to insert all the rows in tblFinalTable after calculating interest based on "LOANMTH" condition.
-- I am a fresher.please help.Thanks in advance.
 
CREATE TABLE dbo.tblmyStoreTable
(
CPFNO int NULL,
LOANMTH int NULL,
EMP_OP int NULL,
L_GRANT int NULL
)

INSERT INTO tblmyStoreTable(100,0,20000,0)
INSERT INTO tblmyStoreTable(200,2,40000,15000)
INSERT INTO tblmyStoreTable(300,0,50000,0)
INSERT INTO tblmyStoreTable(400,5,70000,20000)
INSERT INTO tblmyStoreTable(500,0,100000,0)

CREATE TABLE dbo.tblFinalTable
(
CPFNO int NULL,
LOANMTH int NULL,
EMP_OP int NULL,
L_GRANT int NULL,
EmpInterest INT
)

declare @InterestRate INT
SET @InterestRate=10


IF(@LOANMTH>0)
BEGIN
INSERT INTO tblFinalTable
SELECT CPFNO,LOANMTH,(((EMP_OP-L_GRANT)*@InterestRate/100)/12)*((12-(LOANMTH))+1) AS EmpInterest FROM tblmyStoreTable

END
ELSE
BEGIN
INSERT INTO tblFinalTable
SELECT CPFNO,LOANMTH,(EMP_OP*@InterestRate/100) as EmpInterest FROM tblmyStoreTable
END

Answers (2)