简体   繁体   English

使用insert into时,存储过程中的对象名称无效

[英]Invalid Object Name in Stored Procedure when using insert into

EDIT: 编辑:

I have two tables Table 1 and Table 2 that are linked by GUIDs and I want to search Table 2 for any GUIDs that are not in Table 1 and insert them 我有两个由GUID链接的表Table 1和Table 2,我想在表2中搜索不在表1中的任何GUID并将它们插入

I am trying to use an insert into select from where statement as shown below: 我试图使用插入到select from where语句中,如下所示:

insert into WebCatalog.Published.DemoTracking (RCompanyLink, Purchased, DemoDate)
Select RC.Link, Purchased = 0, DemoDate = GETDATE()
from WebCatalog.Published.RCompany RC
where RC.Link != WebCatalog.Published.DemoTracking.RCompanyLink and RC.DemoLicense = 1

I keep getting an Invalid Object Name error when it is in a stored procedure and a the multipart identifier cannot be bound in reference to 当它在存储过程中并且无法绑定多部分标识符时,我不断收到无效对象名称错误

WebCatalog.Published.DemoTracking.RCompanyLink

error when I try to execute it by itself. 当我尝试自行执行时出现错误。

Am I formatting my statement incorrectly? 我的报表格式有误吗?

Instead of 代替

 RC.Link != WebCatalog.Published.DemoTracking.RCompanyLink 

Use 采用

 RC.Link not in ( Select RCompanyLink from WebCatalog.Published.DemoTracking )

Complete code 完整的代码

INSERT INTO webcatalog.published.demotracking 
            (rcompanylink, 
             purchased, 
             demodate) 
SELECT RC.link, 
       Purchased = 0, 
       DemoDate = Getdate() 
FROM   webcatalog.published.rcompany RC 
WHERE  RC.link NOT IN (SELECT rcompanylink 
                       FROM   webcatalog.published.demotracking) 
       AND RC.demolicense = 1 

The problem is that in SQL you must tell the server how the data relates; 问题在于,在SQL中,您必须告诉服务器数据如何关联。 it doesn't infer it automatically. 它不会自动推断。 What you have looks like it might work in an orm and using Linq2Sql (a .Net technology). 您所看到的内容可能可以在orm中使用Linq2Sql(.Net技术)工作。

I'd recommend using NOT EXISTS which I believe performs better than IN. 我建议使用NOT EXISTS,我认为它的性能要优于IN。

INSERT INTO webcatalog.published.demotracking 
            (rcompanylink, 
             purchased, 
             demodate) 
SELECT RC.link, 
       Purchased = 0, 
       DemoDate = Getdate() 
FROM   webcatalog.published.rcompany RC 
WHERE  NOT EXISTS (SELECT *
         FROM   webcatalog.published.demotracking dt WHERE dt.rcompanylink = RC.link) 
       AND RC.demolicense = 1 

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

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