编辑:
我有两个表,表1和表2通过GUID链接,我想在表2中搜索表1中没有的任何GUID并插入它们
我正在尝试使用insert from where 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
当它在存储过程中时,我不断收到无效对象名称错误,并且多部分标识符无法在引用
时绑定WebCatalog.Published.DemoTracking.RCompanyLink
当我尝试自己执行它时出现错误。
我是否错误地格式化了我的陈述?
答案 0 :(得分: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
答案 1 :(得分: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