定期更新暫存table

  • 47
  • 0

定期更新暫存table

DECLARE  @TH001 Datetime
SET @TH001=(
SELECT
create_date
FROM tempdb.sys.all_objects
WHERE Object_id=Object_id('tempdb..#temp')
)
IF(@TH001 is null)
BEGIN
    SELECT * INTO #temp FROM rack_detail with(nolock) where rack_id='236';
    SELECT * FROM #temp with(nolock) order by cell_no;
END
else 
    BEGIN
        if(DATEDIFF(ms,@TH001,GETDATE())<500)SELECT * FROM #temp order by cell_no;
        else
        BEGIN
        TRUNCATE TABLE #temp;
        INSERT INTO #temp SELECT * FROM rack_detail with(nolock) where rack_id='236';
        SELECT * FROM #temp with(nolock) order by cell_no;
    END; 
END;