Query to Insert record into another table using Select Statement in SQL Server

Last Reply one month ago By pandeyism

Posted one month ago

Hello ,

what i need is to select informattion from certain tables and insert them into another table . insert two tables into total_g1 with id , total , and name of the table .  Regards

SELECT ID, Sum(total) FROM Table1 WHERE Year = 2010 GROUP BY ID

 

SELECT ID, Sum(total) FROM Table2 WHERE Year = 2010 GROUP BY ID

 

INSERT INTO TOTAL_G1 (ID, TOTAL, Name_Table) VALUES ('@id','" @total "','Table1')"

 

INSERT INTO TOTAL_G1 (ID, TOTAL, Name_Table) VALUES ('@id','" @total "','Table2')"

 

Posted one month ago

Hey LoganTRK,

Please refer below query.

SQL

INSERT INTO TOTAL_G1 (ID, TOTAL, Name_Table)
SELECT ID, Sum(total),'Table1' 
FROM Table1 
WHERE Year = 2010 
GROUP BY ID

INSERT INTO TOTAL_G1 (ID, TOTAL, Name_Table)
SELECT ID, Sum(total),'Table2' 
FROM Table2 
WHERE Year = 2010 
GROUP BY ID