Your temp table has an identity column because SELECT .. INTO
replicates the table structure of your original table, which has an identity column.
I would suggest explicitly creating your temporary table, as well as listing the columns you are selecting from department. It is almost never a good idea to use SELECT * in production code, so I would suggest explicitly listing your select and insert into columns:
CREATE TABLE #Department
( PayrollDepartmentID INT NOT NULL,
<more columns>
);
IF @departmentID < 1 OR @departmentID IS NULL
BEGIN
INSERT #Department (PayrollDepartmentID, <more columns>)
SELECT PayrollDepartmentID, <more columns>
FROM PayrollDepartment;
END
ELSE
BEGIN
INSERT #Department (PayrollDepartmentID, <more columns>)
SELECT PayrollDepartmentID, <more columns>
FROM PayrollDepartment
WHERE PayrollDepartmentID = @departmentID;
END
It might seem like more work, and it is definitely more verbose, but it is much better practice and more robust in the long term. It also means you can avoid redundancy by only fetching the columns you actually need from PayrollDepartment