3

我正在尝试创建一个使用表值参数过滤导入名称的存储过程。我们得到一个导入的名称列表,需要返回一个名称列表,这些名称要么不存在于数据库中,要么已经收到特定类型的消息。

我遇到困难的部分是如何创建连接。带有 IS NULL 检查的左外连接适用于单个表检查,但我不确定是否有两个表。任何帮助将不胜感激。谢谢!

我正在使用 MS SQL 2012。带有列的表:

Recipients: 
id | name 

SendResults:
id | recipientid | type

特斯

CREATE PROCEDURE [dbo].[usp_RecipientsSendCheck]
(
     @Type int 
    ,@RecipientsImports dbo.RecipientsImport READONLY
)
AS
BEGIN

    SELECT i.Name FROM @RecipientsImports i

    LEFT JOIN Recipients r
    ON i.Name = r.Name

    LEFT JOIN SendResults s
    ON s.RecipientId = r.id 

    WHERE r.Name IS NULL OR Type <> @Type

END

用户定义的表类型

CREATE TYPE [dbo].[RecipientsImport] AS TABLE(
    [Name] [nchar](10) NOT NULL
)
4

1 回答 1

4

像这样的东西:

create procedure [dbo].[usp_RecipientsSendCheck]
(
    @Type int,
    @RecipientsImports dbo.RecipientsImport READONLY
)
as
begin
    select i.Name
    from @RecipientsImports as i
        left outer join Recipients as r on r.Name = i.Name
        left outer join SendResults as s on s.RecipientId = r.id and s.[Type] = @Type
    where
        r.Name is null or
        s.id is not null
end
于 2013-11-06T18:27:49.523 回答