WITH子句

时间:2017-08-04 13:28:21

标签: sql sql-server

我正在尝试编写类似下面的代码,但却收到错误:

  

如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则前一个语句必须以分号结束。

我尝试插入WITH,但没有帮助。

select @flag = 1 from tabC where 1 = (WITH mul AS
(
   SELECT REPLICATE(CHAR(32 + N), 4) AS val
   FROM (select top 95 row_number() over(order by t1.number) as N
         from   master..spt_values t1)  AS s
)
SELECT *
FROM tabC c
WHERE LEN(CompanyName) > 4
  AND EXISTS (SELECT 1
              FROM mul
              WHERE CHARINDEX(mul.val,c.CompanyName) > 0))

1 个答案:

答案 0 :(得分:2)

您无法嵌入CTE

试试这个

DECLARE @flag bit; --semi colon is important

WITH mul AS
(
   SELECT REPLICATE(CHAR(32 + N), 4) AS val
   FROM (select top 95 row_number() over(order by t1.number) as N
         from   master..spt_values t1)  AS s
)
SELECT @flag = 1
FROM tabC c
WHERE val = 1
  AND LEN(CompanyName) > 4
  AND EXISTS (SELECT 1
              FROM mul
              WHERE CHARINDEX(mul.val,c.CompanyName) > 0)