不可以啊。加了分号提示 Incorrect syntax near ';'. 不加; 提示Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
if (select count(*) from t_eng_partno where ver=@ver and pno=@pno)>0 begin with cte as end
没得写,除非 if (select count(*) from t_eng_partno where ver=@ver and pno=@pno)>0 exec('with cte as ...')
if (select count(*) from t_eng_partno where ver=@ver and pno=@pno)>0 begin ;with cte as () SELEC * FROM CTE END
哦,理解错误,以为是 if with cte .. else with cte .. select * from cte
if(select count(*) from a)>0 begin ;with cte as ( select * from a ) select * from cte enduserid USERname ---------- ---------- 1 aa 2 bb 3 cc(3 row(s) affected)
不加; 提示Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
if (select count(*) from t_eng_partno where ver=@ver and pno=@pno)>0
begin
with cte
as
end
if (select count(*) from t_eng_partno where ver=@ver and pno=@pno)>0
exec('with cte as
...')
begin
;with cte
as
()
SELEC * FROM CTE
END
if
with cte
..
else
with cte
..
select * from cte
begin
;with cte
as
(
select * from a
)
select * from cte
enduserid USERname
---------- ----------
1 aa
2 bb
3 cc(3 row(s) affected)