简体   繁体   English

数据库中已经存在名为'name'的object - 存储过程错误 - SQL 服务器

[英]There is already an object named 'name' in the database - stored procedure error - SQL Server

I have two tables called timeus and usdpay in my SQL Server database.我的 SQL 服务器数据库中有两个名为timeususdpay的表。 These two tables get updated every week.这两个表每周更新一次。

I did small transformation and combined these two tables and created a new table called fluslabor .我做了一些小的改造,将这两个表组合起来,创建了一个名为fluslabor的新表。

I created fluslabor using the stored procedure shown here, and it is working:我使用此处显示的存储过程创建了fluslabor ,它正在运行:

CREATE PROCEDURE [dbo].[sp_uslabor]
AS
BEGIN
    SET NOCOUNT ON

    IF OBJECT_ID(N'dbo.fluslabor', N'U') IS NOT NULL
        TRUNCATE TABLE [dbo].[fluslabor];

    SELECT ut.employee_code
            ,ut.employee_name
            ,ut.department_desc
            ,ut.location_desc
            ,ut.hour 
            ,ut.projects_code
            ,ut.in_effective_time
            ,ut.out_effective_time
            ,ut.date
            ,ut.id  
            ,p.rate
            ,(p.rate * ut.hour ) as Labour_Cost
    INTO fluslabor
    FROM timeus ut
    LEFT JOIN usdpay p ON (TRIM(ut.id) = TRIM(p.id) AND ut.date = p.date)
    WHERE ut.projects_code NOT LIKE '0%'
END

Today I got new data updated in my two tables timeus and usdpay .今天我在我的两个表timeususdpay中更新了新数据。

When I execute my stored procedure, SQL Server is throwing this error:当我执行存储过程时,SQL 服务器抛出此错误:

Msg 2714, Level 16, State 6, Procedure SP_uslabor, Line 12 [Batch Start Line 38]消息 2714,级别 16,State 6,过程 SP_uslabor,第 12 行 [批启动第 38 行]
There is already an object named 'fluslabor' in the database.数据库中已经有一个名为“fluslabor”的 object。

I need to truncate my table every time and load the new data.我每次都需要截断表并加载新数据。 I checked the similar post, they said to use drop table option.我检查了类似的帖子,他们说要使用 drop table 选项。 I don't want to drop the table, just want to truncate and execute the procedure我不想删除表,只想截断并执行程序

Can anyone advise what is the issue here please?谁能告诉我这里的问题是什么?

The problem here is that the table fluslabor already exists in the database.这里的问题是fluslabor表已经存在于数据库中。 what you are trying above the insert is checking the object existence and then truncating the same您在插入上方尝试的是检查 object 是否存在,然后截断相同的内容

There are two possible approaches that you can try here.您可以在此处尝试两种可能的方法。

  1. Instead if the TRUNCATE do a DROP TABLE .相反,如果TRUNCATE执行DROP TABLE But This will also remove the existing user permissions on the table if you have provided specific custom access to the table to any of the users但是,如果您已向任何用户提供对表的特定自定义访问权限,这也将删除表上的现有用户权限

    IF OBJECT_ID(N'dbo.fluslabor', N'U') IS NOT NULL DROP TABLE [dbo].[fluslabor];

  2. The safest approach will be change the SELECT.. INTO statement and convert it into INSERT INTO like this最安全的方法是更改SELECT.. INTO语句并将其转换为INSERT INTO像这样

    INSERT INTO fluslabor ( <List your Destination columns> ) SELECT <List your Source columns> FROM <Source Query>

the 2nd approach will have the records loaded along with keeping all the existing permissions第二种方法将加载记录并保留所有现有权限

IF EXISTS (SELECT 1 FROM sys.objects WHERE type = 'P' AND name = 'your SP name')
BEGIN
    DROP PROCEDURE "your SP name";
END
GO
CREATE PROCEDURE "your SP name"

AS
BEGIN
.
.
.
.

try this one I guess this will help you.试试这个我想这会对你有所帮助。

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

相关问题 存储过程:数据库中已经有一个名为“ #columntable”的对象 - Stored Procedure: There is already an object named '#columntable' in the database SQL Server 中的现有表导致“数据库中已经存在名为‘***’的对象”错误 - Existing table in SQL Server causing 'There is already an object named '***' in the database' error 如何修复“sql server中已存在一个名为&#39;&#39;的数据库中的对象”错误 - How to fix “There is already an object named ' ' in the database” error in sql server 存储过程中出现错误,错误消息为:已经存在一个名为 - Error In Stored Procedure with error message as There is already an object named SQL Server:存储过程中的动态数据库名称 - SQL Server : dynamic database name in stored procedure 无效的对象名称SQL Server存储过程 - Invalid object name sql server stored procedure SQL错误:“数据库中已存在名为XXXX的对象” - SQL Error: “There is already an object named XXXX in the database” SQL 错误:数据库中已经存在名为“TPatients”的 object - SQL Error: There is already an object named 'TPatients' in the database SQL错误-无效的对象名称存储过程 - SQL Error - Invalid Object Name Stored Procedure 使用存储过程检查SQL Server数据库中是否已存在行 - Checking if a row already exists in SQL Server database using stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM