如何在sql server中使用带有OR子句的IF EXISTS语句

时间:2016-01-11 08:37:48

标签: sql sql-server tsql

我们需要创建一个tinymce.get("textfull").getBody().innerHTML = d.Description; scriptinsert仅新created officesdistricts Data table。我们需要检查新Office Districttables Records,如果数据存在,我们需要insert

我们已经尝试了以下语法,

IF EXISTS (
    SELECT 1
    FROM [dbo].[office] o
    LEFT OUTER JOIN [dbo].[Datatable] a ON (o.OfficeID = a.OfficeID)
    WHERE a.OfficeID IS NULL
)
OR (
IF EXISTS (
    SELECT 1
    FROM [dbo].[District] o
    LEFT OUTER JOIN [dbo].[Datatable] a ON (o.DistrictID = DistrictID)
    WHERE a.DistrictID IS NULL
)
)
BEGIN
   -- //CODE
END

但SSMS为此提供了syntax error。如何实现上述场景?

1 个答案:

答案 0 :(得分:6)

应该是... OR EXISTS(而不是OR ( if EXISTS(

IF EXISTS
 (
     SELECT 1
     FROM [dbo].[office] o
     LEFT OUTER JOIN [dbo].[Datatable] a
        ON (o.OfficeID = a.OfficeID )
     WHERE  a.OfficeID IS NULL
 )
OR EXISTS
 (
     SELECT 1
     FROM [dbo].[District] o
     LEFT OUTER JOIN [dbo].[Datatable] a
        ON (o.DistrictID = DistrictID )
     WHERE  a.DistrictID IS NULL
 ) 
BEGIN
    //CODE
END