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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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.