简体   繁体   English

数据库SQL Server中已经存在一个对象

[英]There already exists an object in the database SQL Server

I have created a stored procedure that returns the id of last inserted row of a table based on one condition. 我创建了一个存储过程,该存储过程根据一个条件返回表的最后插入行的ID。

Condition is such that if the row being inserted already exists then it takes identity column of the row otherwise it inserts a new row into the table. 这样的条件是,如果要插入的行已经存在,则它将占据该行的标识列,否则将向表中插入新行。

To do this, I have written the following code in a stored procedure 为此,我在存储过程中编写了以下代码

ALTER PROCEDURE [dbo].[Test_Procedure] 
    @description nvarchar(max) 
AS
BEGIN
    DECLARE @tempId int;

    SELECT CommentId 
    INTO tempId 
    FROM TestTable 
    WHERE description = @description;

    IF @tempId IS NULL
    BEGIN
        INSERT INTO TestTable 
        VALUES (@description);

        SELECT scope_identity();
    END
    ELSE 
    BEGIN
        SELECT @tempId FROM dual;
    END

    DROP TABLE tempId;
END

When I run the above stored procedure, first time it ran successfully and then on wards it started throwing the following error message 当我运行上面的存储过程时,第一次运行成功,然后在病房开始抛出以下错误消息

Msg 2714, Level 16, State 6, Procedure Test_Procedure, Line 15 消息2714,级别16,状态6,过程Test_Procedure,第15行
There is already an object named 'tempId' in the database. 数据库中已经有一个名为“ tempId”的对象。

The bit I'm not understanding is tempId is used as a variable not as a table. 我不明白的一点是tempId用作变量而不是表。 I have seen people with the similar problem but in their case they used temporary tables 我见过有类似问题的人,但在他们的情况下,他们使用了临时表

I really appreciate your help in resolving the above issue. 非常感谢您为解决上述问题所提供的帮助。

Try this syntax for setting your variable. 尝试使用以下语法来设置变量。

SELECT @tempId  = CommentId from TestTable where description = @description;

Currently your 'select into' is creating a table 'tempId' on the database. 当前,您的“选择进入”正在数据库上创建表“ tempId”。

暂无
暂无

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

相关问题 该表已存在于新的SQL Server数据库中 - Table already exists in a new SQL Server database 如何检查SQL Server中已经存在的数据库? 如果数据库已经存在,如何停止查询? - how to check database already exists in sql server? and how stop the query if database already exists? 使用存储过程检查SQL Server数据库中是否已存在行 - Checking if a row already exists in SQL Server database using stored procedure 如何解决 SQL 服务器中的“对象已存在”错误 - How to work around “Object Already Exists” Error in SQL Server SQL Server数据库:找不到明显存在的对象 - SQL Server database: Can't find an object which clearly exists 无法将数据复制到SQL Server-“数据库中已经有一个命名的对象。” - Unable to copy data to SQL server - “There is already an object named in the database.” 如何修复“sql server中已存在一个名为''的数据库中的对象”错误 - How to fix “There is already an object named ' ' in the database” error in sql server SQL Server 中的现有表导致“数据库中已经存在名为‘***’的对象”错误 - Existing table in SQL Server causing 'There is already an object named '***' in the database' error SQL server-declare local variable:“数据库中已经有一个名为''的对象” - SQL server-declare local variable: “there is already an object named '' in the database” 检查 SQL Server 登录名是否已存在 - Checking if a SQL Server login already exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM