2013-05-22 13 views
0

我需要將m.expiry_date分配給變量@nextActionDueOn。向變量賦值的SELECT語句不能與數據檢索操作結合使用

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中,我分配了過期日期並將它作爲參數傳遞給另一個存儲過程。但是我得到了我在標題中給出的錯誤。

+0

[將一個值賦給變量的SELECT語句不能與數據檢索操作結合](http://stackoverflow.com/問題/ 4608140/A-select語句,也就是說-受讓人-a值到一個變量都不能待聯合使用) – Pondlife

回答

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; 

. . . 
相關問題