0

m.expiry_date を変数 @nextActionDueOn に割り当てる必要があります。

ALTER PROCEDURE [dbo].[MembershipRetentionCheck]
AS
BEGIN
    SET NOCOUNT ON;

    declare @firstActionTypeId int = 25 -- membership expiring
    declare @nextActionTypeId int = 3 -- Call company
    declare @nextActionDueOn date

    if object_id('tempdb..#companies') is not null
        drop table #companies

    create table #companies (id int, membLevel int, expiryDate date)

    -- find all companies 
    insert into #companies
    select c.Company_Id, m.MembershipLevel, @nextActionDueOn = m.[Expiry_Date]
    from COMPANY c
        inner join MEMBERSHIP m on c.Company_ID = m.Company_ID
    where 
        -- current member
        m.IsMember_Ind <> 0 and 
        -- membership will expire in one month
        m.[Expiry_Date] between GETDATE() and DATEADD(month, 1, getdate()) and
        -- and don't have an action of this type within the membership period
        not exists(select * from TaskAction ta where 
            (
                (ta.FirstActionTypeId = @firstActionTypeId) -- first action is membership expiring
                or  (ta.TaskTypeId = 8) -- Retention (user created) = 8 FB3367
                -- look for Retention task now assigned to Artwork FB3367
                or (ta.TaskTypeId in (37, 39, 41) and ta.CurrentEditRecord like '%Subject changed from "Retention%')
            ) and
            ta.EntityId = c.Company_ID and
            ta.EntityTypeId = 1 and 
            ta.TaskCreatedOn between DATEADD(MONTH, -2, m.[Expiry_Date]) and GETDATE()
        )

    exec CreateRetentionTasks @firstActionTypeId, @nextActionTypeId, @nextActionDueOn

    drop table #companies
END

この SP では、有効期限を割り当て、それをパラメーターとして別のストアド プロシージャに渡しています。しかし、タイトルで指定したエラーが発生しています。

4

1 に答える 1

2

複数の行からの複数の値である可能性がある変数に割り当てているため、クエリは意味がありません。有効期限を一時テーブルに入れるのはどうですか:

. . .

create table #companies (id int, membLevel int, Expiry_Date datetime);

-- find all companies 
insert into #companies
select c.Company_Id, m.MembershipLevel, m.[Expiry_Date]
from COMPANY c
    inner join MEMBERSHIP m on c.Company_ID = m.Company_ID
where 
    -- current member
    m.IsMember_Ind <> 0 and 
    -- membership will expire in one month
    m.[Expiry_Date] between GETDATE() and DATEADD(month, 1, getdate()) and
    -- and don't have an action of this type within the membership period
    not exists(select * from TaskAction ta where 
        (
            (ta.FirstActionTypeId = @firstActionTypeId) -- first action is membership expiring
            or  (ta.TaskTypeId = 8) -- Retention (user created) = 8 FB3367
            -- look for Retention task now assigned to Artwork FB3367
            or (ta.TaskTypeId in (37, 39, 41) and ta.CurrentEditRecord like '%Subject changed from "Retention%')
        ) and
        ta.EntityId = c.Company_ID and
        ta.EntityTypeId = 1 and 
        ta.TaskCreatedOn between DATEADD(MONTH, -2, m.[Expiry_Date]) and GETDATE()
    );

select  @nextActionDueOn = max(c.[Expiry_Date])
from #Companies c;

. . .
于 2013-05-22T14:29:48.723 に答える