CTE EROOR MESSAGE -DELETE 2 TABLE RECORDS

hi experts,
I'm getting error message

error
Msg 208, Level 16, State 1, Line 20
Invalid object name 'withdelte'.

;withdelte
as
(
select a.ID
from a
inner join b on a.id=b.id

)

delete from c where ID IN( select ID from withdelte)

delete from d where ID IN( select ID from withdelte)
delete from f where ID IN( select ID from withdelte)

Open in new window


its working first delete but not others,any idea appriciated
LVL 2
lankapalaAsked:
Who is Participating?
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.

Mark WillsTopic AdvisorCommented:
for a Common Table Expression (or CTE for short) it starts with a "WITH" then you give it a name

So your first line is missing a space....

We also often prefix with a semicolon because a CTE must exist as a first line in a batch (which you have done)

But you wont be able to run three seperate delete statements, you can only have one.

So, change it to
;with cte_delete
as
(
select a.ID
from a
inner join b on a.id=b.id

)

delete from c where ID IN( select ID from cte_delete)

--delete from d where ID IN( select ID from withdelte)
--delete from f where ID IN( select ID from withdelte)

Open in new window


Now, to get the other two deletes you could repeat for the above for the other two tables, or, you have to "capture" those ID's. So better approach would be
;with cte_delete
as
(
select a.ID
from a
inner join b on a.id=b.id

) select id into #cte_delete from cte_delete

delete from c where ID IN( select ID from #cte_delete)
delete from d where ID IN( select ID from #cte_delete)
delete from f where ID IN( select ID from #cte_delete)

Open in new window


And if you want to rerun in the same session, dont forget to delete the temp table #cte_delete

have a read of CTE : https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-2017
0

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
lankapalaAuthor Commented:
Thanks
0
lankapalaAuthor Commented:
Great
0
Mark WillsTopic AdvisorCommented:
Glad you liked it, and my pleasure in being able to help :)
0
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
Query Syntax

From novice to tech pro — start learning today.