不对应的价值观

时间:2015-03-10 13:34:34

标签: sql-server stored-procedures

嘿我正在尝试创建一个存储过程,该过程将向我返回尚未添加到组中的所有属性的信息。

我有两张桌子:

GroupDetails

InspectionGroupID    PropertyID
9                    12
8                    13
6                    14
10                   25

PropertyMaster

PropertyID      ManagerID      AddressLine 1
12                  2          34 Easter Road   
13                  0          104 Montgomery Street    
14                  0          67 Holyrood Road 
15                  1          45 Lala
16                  0          34 Uptown

我一直在想这样的事情,但它不起作用。我在树视图上也有一个搜索过滤器,这就是@SearchValue的原因。

Alter PROCEDURE [dbo].[GroupManagmentProperties_LoadRecords]
        @SearchValue varchar(30) = NULL

    AS
    BEGIN
    SET NOCOUNT ON;
SELECT        PropertyMaster.PropertyID, PropertyMaster.AddressLine1, InspectionGroupDetails.PropertyID
FROM            PropertyMaster INNER JOIN
                         InspectionGroupDetails ON PropertyMaster.PropertyID = InspectionGroupDetails.PropertyID WHERE AddressLine1 LIKE '%' + @SearchValue + '%' and PropertyMaster.PropertyID <> InspectionGroupDetails.PropertyID  
ORDER BY AddressLine1

END

1 个答案:

答案 0 :(得分:2)

如果您要查看未引用的记录,则需要左侧或右侧的外部联接。这仍然会产生记录,然后您可以检查找到的参考文献并进行适当的过滤。

Alter PROCEDURE [dbo].[GroupManagmentProperties_LoadRecords]
(
    @SearchValue varchar(30) = NULL
)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        PropertyMaster.PropertyID, PropertyMaster.AddressLine1, InspectionGroupDetails.PropertyID
    FROM PropertyMaster
    LEFT JOIN InspectionGroupDetails ON PropertyMaster.PropertyID = InspectionGroupDetails.PropertyID
    WHERE InspectionGroupDetails.PropertyID IS NULL
        AND AddressLine1 LIKE '%' + @SearchValue + '%'  
    ORDER BY AddressLine1;
END