从另一个存储过程访问在存储过程中创建的## TempTable

时间:2019-12-01 20:16:39

标签: sql sql-server sql-delete temp-tables global-temp-tables

我有两个存储过程来删除记录。我需要在SP1中删除记录的ID,以便在SP2中删除时查询它们。我创建了一个全局临时表以将ID保存在SP1中,并且需要从SP2访问此##TempTable才能完成删除,这是否可能。我收到语法错误。

这是我的代码。在第一个SP1中,我必须删除一些recored,并将删除的recoreds保存到文件中,因此我创建了一个临时表。

CREATE TABLE ##DeletedInsurance(
[InsuranceID] [int] NULL,
[InsuranceTypeID] [int] NULL,
[MemberID] [int] NULL)

    DELETE Insurance
    OUTPUT DELETED.* INTO ##DeletedInsurance
    FROM Insurance 
     inner join member on Insurance.MemberID = Member.MemberID 
     WHERE Insurance.MemberID IN(
     SELECT DISTINCT Member.MemberID                
                FROM Insurance                  
                    WHERE (
                          (Insurance.insuranceid is null and 
                Insurance.LastChangedDateTime <@archieveDate ) 
                          OR Insurance.ResignationDate < @archieveDate)) 

然后我删除呼叫SP2,在这里我必须删除记录,但我需要在保险表(它是子表)中删除已删除的ID,并且需要在案例表之前删除它

    DELETE Case
    OUTPUT DELETED.* INTO ##Case
    FROM Case ac 
    INNER JOIN Member m ON ac.MemberID = m.MemberID
    INNER JOIN Insurance i ON i.MemberID = m.MemberID
    WHERE ac.MemberID IN(
     SELECT DISTINCT m.MemberID         
                FROM Insurnace
                    WHERE (
                          (i.insuranceid IS NULL AND m.LastChanged<@arcDate)                )) 

0 个答案:

没有答案