删除加入声明

时间:2012-02-01 14:05:18

标签: sql sql-server sql-delete

我正在尝试在我的存储过程中使用Delete语句但是它给我一个错误说,

无效的对象名称'BRWSQLDC'。

以下是我的删除声明:

set @Query = 'DELETE FROM ' + @DestLinkServer + ' FROM .HL2_61.dbo.ArtPDF AP JOIN .HL2_61.dbo.Article A on A.ArticleID = AP.ArticleID ' + ' WHERE ArticleKey = ' + CONVERT(VARCHAR, @Id)

当我执行下面的

DELETE FROM BRWSQLDC FROM .HL2_61.dbo.ArticlePDF AP JOIN .HL2_61.dbo.Article A on A.ArticleID = AP.ArticleID  WHERE ArticleKey = -1591276581
  

错误是:无效的对象名称'BRWSQLDC'。

如果我尝试执行如下代码:

'DELETE FROM ' + @DestLinkServer + ' .HL2_61.dbo.ArticlePDF AP JOIN .HL2_61.dbo.Article A on A.ArticleID = AP.ArticleID ' + ' WHERE ArticleKey = ' + CONVERT(VARCHAR, @Id)

传递值时,

DELETE FROM BRWSQLDC .HL2_61.dbo.ArticlePDF AP JOIN .HL2_61.dbo.Article A on A.ArticleID = AP.ArticleID  WHERE ArticleKey = -1591276581
我得到的错误是:

  

'AP'附近的语法不正确。

请帮我如何在删除中加入2个表,然后在服务器中删除它(如果存在)。

3 个答案:

答案 0 :(得分:4)

您不需要连接,而是需要正确的WHERE子句。

答案 1 :(得分:3)

删除后将别名移至,但您还应识别ArticleKey来自的表

DELETE AP
FROM BRWSQLDC.HL2_61.dbo.ArticlePDF AP 
JOIN BRWSQLDC.HL2_61.dbo.Article A   -- preface this with your server name for clarity
    ON A.ArticleID = AP.ArticleID  
WHERE A.ArticleKey = -1591276581 

答案 2 :(得分:1)

当您使用联接时,您必须告诉您要从哪个表中删除行:

DELETE dbo.Customer
FROM dbo.Customer cust INNER JOIN dbo.Person pers ON pers.ID_CUST = cust.ID_CUST