我正在尝试创建一个存储过程,该过程将能够限制使用部门 ID 返回的记录数。我试图通过加入临时表来限制记录。
当我运行下面的代码时,我得到了错误:
只有在使用列列表并且 IDENTITY_INSERT 为 ON 时,才能为表“#department”中的标识列指定显式值。
这是代码:
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
我开始:
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
但我得到了错误:
数据库中已经有一个名为“#department”的对象
我不是 SQL 专家,所以也许有更好的方法来做到这一点?