How to insert data to a table from another table programmatically (using TSQL)?

There can be two possible scenarios including if target table exists or if it does not exist.

Please use appropriate method as per your requirement:

Method 1: If target table does not exist

Please use below code:

USE <Database>
SELECT <Colume 1>, <Colume 2>
INTO <Target Table>
FROM <Source Table>
WHERE <Condition>

This method will create new table with the same definition of source table. If you use select * instead of specific column name, it will actually copy the whole source table.

Method 2: If target table exist

Please use below code:

INSERT INTO Targettable (<Col1>, <Col2>)
SELECT <Col1>, <Col2>
FROM SourceTable
WHERE <Condition>

You need to ensure that data type matches in source and destination column.