Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9313213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:49:29+00:00 2026-06-19T01:49:29+00:00

I am a newb to SQL since I can normally get away with simple

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:49:30+00:00Added an answer on June 19, 2026 at 1:49 am

    Your DELETE statements don’t reference aDeleteVariable. The id column only exists in the context of aDeleteVariable.

    You need to join aDeleteVariable into your DELETE statements. 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.

    WITH aDeleteVariable AS (SELECT TOP 1 FooID as id
            FROM [BSystem].[Foo].[bar] order by CreateTimeUTC desc)
    DELETE FB 
            FROM [BSystem].[Foo].[fooBar] AS FB
            INNER JOIN aDeleteVariable AS DV ON (FB.FooID = DV.id);
    WITH aDeleteVariable AS (SELECT TOP 1 FooID as id
            FROM [BSystem].[Foo].[bar] order by CreateTimeUTC desc)
    DELETE B
            FROM [BSystem].[Foo].[bar] AS B
            INNER JOIN aDeleteVariable AS DV ON (B.FooID = DV.id);
    

    Of course, with joining to aDeleteVariable, you don’t need the id alias. The following will work.

    WITH aDeleteVariable AS (SELECT TOP 1 FooID
            FROM [BSystem].[Foo].[bar] order by CreateTimeUTC desc)
    DELETE FB 
            FROM [BSystem].[Foo].[fooBar] AS FB
            INNER JOIN aDeleteVariable AS DV ON (FB.FooID = DV.FooID);
    WITH aDeleteVariable AS (SELECT TOP 1 FooID
            FROM [BSystem].[Foo].[bar] order by CreateTimeUTC desc)
    DELETE B
            FROM [BSystem].[Foo].[bar] AS B
            INNER JOIN aDeleteVariable AS DV ON (B.FooID = DV.FooID);
    

    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 to N records). You might want to select your IDs into a table variable instead of using a common table expression. The following should work.

    DECLARE @DeleteVariable TABLE (FooID INT);
    INSERT INTO @DeleteVariable (FooID)
            SELECT TOP 1 FooID FROM [BSystem].[Foo].[bar]
                    ORDER BY CreateTimeUTC DESC;
    DELETE FB 
            FROM [BSystem].[Foo].[fooBar] AS FB
            INNER JOIN @DeleteVariable AS DV ON (FB.FooID = DV.FooID);
    DELETE B
            FROM [BSystem].[Foo].[bar] AS B
            INNER JOIN @DeleteVariable AS DV ON (B.FooID = DV.FooID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this should be pretty simple, but I'm a SQL newb. I have
Git newb question here. Coming from SVN, where I can have multiple branches checked
Newb question here: how can I have the value stored in Maptest[2] update along
1) For the purpose of really low hash collision, can I get away with
Newb question. How can you know what the main launch Activity is? Learning Android.
I apologize for the newb question but have not found a solution online. I
I'm a PHP newb. Sorry if this is an FAQ... Let's say I have
I'm a total newb to LINQ. Here is the code I have so far:
I have an sql server database, that I pre-loaded with a ton of rows
first time poster - semi-newb to Rails. Here is my question. Suppose you have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.