Wednesday, February 21, 2024

How to create Temporary table in SQL - Temp table - with drop temp table

 WITH OfficeHierarchy AS 

    (

        -- Anchor member (initial data)

        SELECT TableID, OffName, OffType, ParentID

        FROM M_OfficeMaster

        WHERE TableID = @OfficeID  

)

SELECT * FROM OfficeHierarchy


Not :- This above will not work in if and else conditions


Use below code

SELECT 

ChargeSheetId

,CourtCaseNumber

,[Date] AS ChSheetDate

,DisposalDate

,CnrNumber

INTO #ChargeSheets

FROM ChargeSheet 

WHERE OfficeId= @OfficeId AND FIRNumber = @RCNumber 

AND IsActive=1;


DROP TABLE IF EXISTS #ChargeSheets

No comments:

Post a Comment