I'm trying to create a stored procedure that will be able to limit the number of records returned by using the department id. I'm trying to limit the records by joining to a temp table.
When I run the code below I get the error:
An explicit value for the identity column in table '#department' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Here's the code:
DECLARE @departmentID INT
SET @departmentID = 4
-- create temp department table --
select top 0 * into #department from PayrollDepartment
-- load temp department table
IF @departmentID < 1 OR @departmentID IS NULL
BEGIN
INSERT INTO #department SELECT * FROM PayrollDepartment
END
ELSE
BEGIN
INSERT INTO #department SELECT * FROM PayrollDepartment WHERE PayrollDepartmentID = @departmentID
END
I started with:
IF @departmentID < 1 OR @departmentID IS NULL
BEGIN
SELECT * INTO #department FROM ApplicationEmployeeInfo..PayrollDepartment
END
ELSE
BEGIN
SELECT * INTO #department FROM ApplicationEmployeeInfo..PayrollDepartment WHERE PayrollDepartmentID = @departmentID
END
but I get the error:
There is already an object named '#department' in the database
I'm not a SQL expert, so maybe there's a better way to do this?