CTE's only valid for a single operation? Are there alternatives?

I'm have been performing a MERGE operation to push data from a production database into a reporting database on a daily basis (05:00AM) but this process has recently started taking well over an hour.  I posted here the other day about this and the response was that I should consider performing a delete and corresponding append rather than using the MERGE with If Found and If not Found operation.

I wrote an SP which looks something like the following (where "FieldList" is actually a list of fields).

;WITH Recent_Production (FieldList)
SELECT FieldList
FROM ProductionTable

DELETE FROM tbl_Reports
WHERE Exists (SELECT 1 FROM Recent_Production  
WHERE Entity_ID = tbl_Reports.Entity_ID AND ProductionDate = tbl_Reports.docDate)

INSERT INTO tbl_Reports
SELECT * FROM Recent_Production

Open in new window

But this does not work, because the CTE is not found in the second operation (INSERT command).  I've never used CTE's before, but I was under the impression that one of the reasons for using CTEs was to eliminate the need to define a recordset more than once in a procedure.   I've recently discovered that the CTE is only valid for the first operation after it is defined, which is why it works for either the DELETE or the INSERT, when I remark out the other one, but does not work as indicated above.

I've modified my SP to use a temp table rather than the CTE, and everything is working fine now.

Is there another option for dong this?
LVL 52
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
>CTE's only valid for a single operation?

>Are there alternatives?
Yes.  You can define a temporary table such as #rp which will remain in scope throughout the procedure, meaning it can be used in multiple statements.
IF OBJECT_ID('tempdb..#rp') IS NOT NULL

CREATE TABLE #rp (FieldList varchar(100))

INSERT INTO #rp (FieldList) 
SELECT FieldList
FROM ProductionTable

-- Not entirely sure what you're doing here so I'll not edit this beyond #rp
DELETE FROM tbl_Reports
WHERE Exists (SELECT 1 FROM #rp) 
WHERE Entity_ID = #rp.Entity_ID AND ProductionDate = tbl_Reports.docDate)

INSERT INTO tbl_Reports (ReportList) 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Thanks for the confirmation, Jim.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.