JOIN上的UPDATE语句运行时没有错误,但没有任何反应

时间:2016-05-10 16:49:14

标签: sql-server

我正在尝试根据两个单独的表(表人和发票)的日期范围以及所有具有源“purch”(来自人员表)的日期范围来更新表。请参阅以下内容:

UPDATE Person
SET Person.pers_ThankYou_Letter='Y',
    Person.pers_newpurch='recievedLetter',
    Person.pers_update='2016-05-09' 
FROM Person T1
INNER JOIN dbo.invoice T2
ON T1.Pers_PersonId = T2.invoice_personid
WHERE
    T1.Pers_CreateDate >= '2016-02-16'
AND
T2.invoice_InvDate > '2016-02-16'
AND
T1.Pers_Source = 'Purch';

我正在使用SQL Server Management Studio 2005.我尝试了其他各种方法

SELECT * FROM Person
UPDATE Invoice a JOIN Person b ON (a.personid = b.Pers_PersonId)
    SET pers_ThankYou, 
    Person.pers_update='2016-05-09',
    pers_newpurch='recievedLetter',
    pers_update='2016-05-09'
WHERE
Pers_CreateDate >='2016-02-16'
AND
Invoice.InvDate > '2016-02-16'
AND
Pers_Source = 'Purch';

但没有运气。也一直在检查类似的问题,但我已经花了几天时间在这个问题上,所以提前谢谢你。

1 个答案:

答案 0 :(得分:0)

我认为您的UPDATE语句需要修改为不在T1子句中定义别​​名FROM,或在SET子句中使用它。 (见https://technet.microsoft.com/en-us/library/ms187455(v=sql.105).aspx)。

要使用别名,请按以下方式更改代码:

UPDATE T1
SET T1.pers_ThankYou_Letter='Y',
    T1.pers_newpurch='recievedLetter',
    T1.pers_update='2016-05-09' 
FROM Person T1
INNER JOIN dbo.invoice T2
ON T1.Pers_PersonId = T2.invoice_personid
WHERE
    T1.Pers_CreateDate >= '2016-02-16'
AND
T2.invoice_InvDate > '2016-02-16'
AND
T1.Pers_Source = 'Purch';