Bishoy Frank Bishoy Frank - 6 months ago 27
SQL Question

How to add temp table to another temp table with extra column

CREATE TABLE #EmpPcodes
(
YearMonth INT,
YEAR INT,
MONTH INT,
RunNo INT,
Amount NUMERIC(18, 3),
GroupCode NvarCHAR(30),
GroupName NvarCHAR(250),
GroupAName NvarCHAR(250),
PayrollGroup INT,
EmployeeId INT
)
CREATE TABLE #pArrangeAllcode
(
YearMonth INT,
YEAR INT,
MONTH INT,
RunNo INT,
Amount NUMERIC(18, 3),
GroupCode NvarCHAR(30),
GroupName NvarCHAR(250),
GroupAName NvarCHAR(250),
PayrollGroup INT,
EmployeeId INT,
CodeArrange INT
)

INSERT INTO #pArrangeAllcode
SELECT YearMonth, YEAR, MONTH, RunNo, Amount, GroupCode, GroupName,
GroupAName, PayrollGroup, EmployeeId,
FROM dbo.#EmpPcodes

SELECT * FROM #pArrangeAllcode


I expect to get the data from the #EmPcodes temp table to #pArrangeAllcode
but it has extra column that I'll use it later ... I always get this error :

Insert Error: Column name or number of supplied values does not match table definition.


Any Help ?

Answer

Actually you are missing one column in insert statement whch also need some value:

use this:

INSERT INTO #pArrangeAllcode 
SELECT YearMonth, YEAR, MONTH, RunNo, Amount, GroupCode, GroupName, 
GroupAName, PayrollGroup, EmployeeId,NULL
FROM dbo.#EmpPcodes