0

编辑:

我有两个由 GUID 链接的表表 1 和表 2,我想在表 2 中搜索任何不在表 1 中的 GUID 并插入它们

我正在尝试使用 insert into 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

我在存储过程中不断收到 Invalid Object Name 错误,并且多部分标识符无法绑定到

WebCatalog.Published.DemoTracking.RCompanyLink

当我尝试自己执行它时出错。

我的陈述格式不正确吗?

4

2 回答 2

1

代替

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

利用

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

完整代码

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 
于 2013-10-22T16:18:22.980 回答
0

问题是在 SQL 中你必须告诉服务器数据是如何关联的;它不会自动推断它。你所拥有的看起来可能在 orm 中工作并使用 Linq2Sql(一种 .Net 技术)。

我建议使用 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 
于 2013-10-22T16:24:18.537 回答