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 回答