Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql CTE-靠近“.”的语法不正确_Sql_Sql Server - Fatal编程技术网

Sql CTE-靠近“.”的语法不正确

Sql CTE-靠近“.”的语法不正确,sql,sql-server,Sql,Sql Server,这是我的密码: With table_CTE(a.PolicyNumber, Amount, b.PolicyNumber, FundValue) as ( Select a.PolicyNumber, Amount, b.PolicyNumber, FundValue from ( Select PolicyNumber, Amount, from table.a ) as a Full Outer Join (Sele

这是我的密码:

  With table_CTE(a.PolicyNumber, Amount, 
b.PolicyNumber, FundValue)
 as
(
Select a.PolicyNumber, Amount,
b.PolicyNumber, FundValue
    from ( Select PolicyNumber, Amount, 
    from table.a
       ) as a
    
    Full Outer Join
        (Select PolicyNumber, FundValue
        from table.b
       ) as b
        on
    a.PolicyNumber = b.PolicyNumber
)
我正在尝试创建CTE语句,但不断出现错误:
“.”附近的语法不正确。

列名不支持句点。也许你打算:

With table_CTE (PolicyNumber, InvestmentCode, IndexType, Amount, FundID, 
PolicyNumber, FundValue, DepositDate, FundID, [Difference])
也就是说,您不必在CTE定义中重复列别名。它们都是在选择中指定的,您可以使用:

with table_CTE as (
    select . . .
试试下面

    With table_CTE as (Select INDCONEX.PolicyNumber, INDCONEX.InvestmentCode, 
     IndexType, Amount, INDCONEX.FundID, 
     INTCRDEX.PolicyNumber, FundValue, DepositDate, INTCRDEX.FundID, [Difference])

      (
      Select INDCONEX.PolicyNumber, INDCONEX.InvestmentCode,IndexType, Amount, 
      INDCONEX.FundID, 
       INTCRDEX.PolicyNumber, FundValue, DepositDate, INTCRDEX.FundID,
        abs (INDCONEX.Amount-INTCRDEX.FundValue) as [Difference]
    from ( Select PolicyNumber, InvestmentCode, IndexType,Amount, FundID 
   from FinancialExtracts.dbo.INDCONEX
        where RunDate = '20200531' and InvestmentCode >3) as INDCONEX

Full Outer Join
    (Select PolicyNumber, FundValue, DepositDate, FundID
    from FinancialExtracts.dbo.INTCRDEX
        where RunDate = '20200531' and InvestmentCode > 3) as INTCRDEX 
    on
INDCONEX.PolicyNumber = INTCRDEX.PolicyNumber and
INDCONEX.FundID = INTCRDEX.FundID

不应使用cte中使用的表名别名限定cte标题中的列名。 表.a和表.b也是无效的表名,因为它们包含。。 如果这些是实际名称,则应将其括在方括号内:[table.a]和[table.b]。 但是我相信你只是想给他们使用别名,这不是正确的方法。 你把cte复杂化了。 就这么简单:

with table_CTE as (
  select a.PolicyNumber PolicyNumbera, a.Amount,
         b.PolicyNumber PolicyNumberb, b.FundValue
  from tablea as a full outer join tableb as b
  on a.PolicyNumber = b.PolicyNumber
)
用实际的表名替换表A和表B。
列a.PolicyNumber和b.PolicyNumber返回的列具有相同的名称,这是不允许的,这就是为什么我将它们别名为PolicyNumbera和PolicyNumberb。

我只是在学习SQL,所以感谢您的回答。当我运行这段代码时,我收到错误消息,称无法为大多数对象绑定多部分标识符INDCONEX.PolicyNumber。你知道这个错误是什么意思吗?知道!您已经写入了两次策略号—只需写入一次,并且在CTE定义中不使用列别名。上面的查询是从两个不同的表INDCONEX和INTCRDEX中提取列,并基于策略号将它们合并,因为这两个表具有相同的策略号。当我只列出PolicyNumber一次时,我收到一个错误,说table_CTE的列数大于column list中指定的列数。我如何列出这两个表中列出的列PolicyNumber,FundID?在select语句中,您也写了一次?选择INDCONEX.PolicyNumber、INDCONEX.InvestmentCode、IndexType、Amount、INDCONEX.FundID、INTCRDEX.PolicyNumber、FundValue、DepositDate、INTCRDEX.FundID、abs INDCONEX.Amount-INTCRDEX.FundValue作为[Difference],也可以在带。。。。像必须有一个SQL语句使用/引用CTE。。。。