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

  • Home
  • SEARCH
  • 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 8281023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:53:28+00:00 2026-06-08T09:53:28+00:00

I was curious why I can’t seem to get a conditional statement to follow

  • 0

I was curious why I can’t seem to get a conditional statement to follow a common table expression in t-SQL statement?

Stuff like this:

WITH ctx AS(...)
IF ctx.v BEGIN
INSERT INTO ...
END

I made a sample fiddle here.

  • 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-08T09:53:30+00:00Added an answer on June 8, 2026 at 9:53 am

    A common table expression is basically just a subquery that can be reused in multiple SQL statements. So, you would need to select from it in any case; you can’t just reference it like it’s a variable. Think of it more like a temporary table.

    You should be able to achieve what you want just by adding your criteria as a where clause.

    WITH ctx AS(
       SELECT MIN(t1.dtIn) AS mdIn, MAX(t1.dtOut) AS mdOut FROM tbl t1
          INNER JOIN tbl t2
          ON t1.Type = t2.Type
            AND t1.dtIn < t2.dtOut 
            AND t1.dtOut > t2.dtIn
          WHERE t1.type = 1
    )
    
    INSERT INTO tbl (id, dtIn, dtOut, type)
    SELECT 10, 
           CASE WHEN mdOut IS NOT NULL AND 
                     mdOut > '0:30' AND
                     '0:40' >= mdOut THEN mdOut
           ELSE '0:30' END,
           CASE WHEN mdIn IS NOT NULL AND
                     mdIn < '0:40' AND
                     '0:30' >= mdIn THEN mdIn
           ELSE '0:40' END,
    1
    FROM ctx
    where ctx.mdIn < '0:40' AND ctx.mdOut > '0:30'
    
    select * from tbl
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am curious if I can get some help with Open Graph since I
Just curious, is there a format string I can use to output something like
Curious if this can be simplified... internal static IEnumerable<string> Split(string str, char sep =
I am just curious if someone can get me pointed in the right direction
I've always been curious: how can I perform arithmetic operations on very long decimals--for
I'm curious if someone can point me in the right direction here. I'm learning
I'm trying to setup a build file and I was curious if you can
I'm no expert in parallel programming. But I'm curious if I can use them
I'm currently using NVelocity to render some email templates. I'm curious how I can
I can't find if it is or not and am very curious - if

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.