I am a newb to SQL since I can normally get away with simple queries/migrations without digging into it, however I have a more complex one that doesn’t involve a join and I am wondering how I delete when there is a foreign constraint without using on cascade. Here’s my query. I could not find a good answer from googling.
WITH aDeleteVariable AS (SELECT TOP 1 FooID as id FROM [BSystem].[Foo].[bar] order by
CreateTimeUTC desc)
DELETE FROM [BSystem].[Foo].[fooBar] where FooID = id
DELETE FROM [BSystem].[Foo].[bar] where FooID = id
So I need to delete [BSystem].[Foo].fooBar first, then [BSystem].[Foo].[bar] This will be executed at run time so it cannot be hard coded. “1” is really going to be n, however I have that code worked out.
I get invalid column name ‘id’ when trying to execute this query. How can I fix it?
Your
DELETEstatements don’t referenceaDeleteVariable. Theidcolumn only exists in the context ofaDeleteVariable.You need to join
aDeleteVariableinto yourDELETEstatements. Also, you can’t reuse the common table expression, so you have to duplicate it.See this SQL Fiddle (with simplified schema/table names), but below is what you want.
Of course, with joining to
aDeleteVariable, you don’t need theidalias. The following will work.Given that there are two separate common table expressions, there is a race condition, and the second expression might return a different
FooID(or multiple when you adapt this toNrecords). You might want to select your IDs into a table variable instead of using a common table expression. The following should work.