[英]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 服务器数据库中有两个名为timeus
和usdpay
的表。 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
.今天我在我的两个表timeus
和usdpay
中更新了新数据。
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.您可以在此处尝试两种可能的方法。
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];
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.