为变量赋值的SELECT语句不能与数据检索操作结合使用

时间:2013-05-22 14:26:45

标签: sql sql-server sql-server-2008 tsql

我需要将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中,我将分配到期日期并将其作为参数传递给另一个存储过程。但是我收到了标题中给出的错误。

1 个答案:

答案 0 :(得分: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;

. . .