简体   繁体   English

SQL Server 2008 R2-为什么我的临时表认为它具有标识列?

[英]SQL Server 2008 R2 - why does my temp table think it has an identity column?

I'm trying to create a stored procedure that will be able to limit the number of records returned by using the department id. 我正在尝试创建一个存储过程,该存储过程将能够限制使用部门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. 仅当使用列列表且IDENTITY_INSERT为ON时,才能为表'#department'中的identity列指定一个显式值。

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 数据库中已经有一个名为“ #department”的对象

I'm not a SQL expert, so maybe there's a better way to do this? 我不是SQL专家,所以也许有更好的方法可以做到这一点?

Your temp table has an identity column because SELECT .. INTO replicates the table structure of your original table, which has an identity column. 临时表具有一个标识列,因为SELECT .. INTO复制了具有标识列的原始表的表结构。

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: 在生产代码中使用SELECT *几乎从来不是一个好主意 ,因此,我建议明确列出您的选择并插入列中:

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 这也意味着您可以通过仅从PayrollDepartment获取实际需要的列来避免冗余

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM