2016-08-11 37 views
-4

我有一個簡單的問題:在這段代碼中,我不能使用別名,我不知道爲什麼。在SQL中使用別名與類似的語句

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在類似statment

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

我用你們sugested的改變有錯誤我現在它的工作原理。

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 
+2

發佈完整查詢要清楚。 –

+0

TSQL語法不允許在WHERE或ON子句中使用別名。 – Serg

+0

我將別名移至了select,並且錯誤現在在LIKE中 –

回答

2

只需刪除列別名即可。 WHERE子句不是您定義列別名的地方。 SELECT條款適用於:

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