在SQL中使用Alias和类似的语句

时间:2016-08-11 13:39:43

标签: sql-server tsql

我有一个简单的问题:在此代码中,我无法使用别名,我也不知道原因。

tblSaida AS (
            SELECT DISTINCT
                [FI].COD_Regional,
                [FI].COD_Filial,
                [FI].Nom_Abvd_Filial
                FROM
                COR_Filial [FI]
                    INNER JOIN
                tblFiliais [F]
                    ON
                [FI].COD_Regional = [F].COD_Regional
                AND [FI].COD_Filial = [F].COD_Filial
                AND [FI].Cod_RegionalAtual = @Cod_Regional
                AND [FI].Nom_Abvd_Filial LIKE '%' +  ISNULL(@Filtro, Nom_Abvd_Filial) + '%'   AS [NOM_Regional]
                ORDER BY
                [FI].NOM_Regional

                SELECT 
            [S].COD_Filial,
            [S].NOM_Regional

我在' AS'

  

关键字' AS'

附近的语法不正确

有人可以帮助我吗?

编辑:

我将别名移到了select.Now我在同样的语句中有错误

tblSaida AS (
            SELECT DISTINCT
                [FI].COD_Regional,
                [FI].COD_Filial,
                [FI].Nom_Abvd_Filial LIKE '%' +  ISNULL(@Filtro, Nom_Abvd_Filial) + '%'   AS [NOM_Regional]
                FROM

我使用了你们提出的改变,现在它可以使用了。

tblSaida AS (
            SELECT DISTINCT
                [FI].COD_Regional,
                [FI].COD_Filial,
                [FI].Nom_Abvd_Filial
                FROM

                COR_Filial [FI]

                    INNER JOIN
                tblFiliais [F]
                    ON
                [FI].COD_Regional = [F].COD_Regional
                AND [FI].COD_Filial = [F].COD_Filial
                AND [FI].Cod_RegionalAtual = @Cod_Regional

                )
                SELECT 
            [S].COD_Filial,
            [S].Nom_Abvd_Filial



        FROM
            tblSaida [S]
             ORDER BY 
      [S].Nom_Abvd_Filial 

1 个答案:

答案 0 :(得分:2)

只需删除列别名即可。 WHERE子句不是您定义列别名的地方。 SELECT条款适用于此:

[FI].Nom_Abvd_Filial LIKE '%' +  ISNULL(@Filtro, Nom_Abvd_Filial) + '%'