在SQL Server中创建视图的问题

时间:2016-04-08 08:23:13

标签: sql-server syntax relational-database

你可以帮我解决这个问题吗?我只是一个使用Microsoft SQL Server的初学者,我试图像在Oracle中一样创建一个视图。

但它会在关键字select"附近导致错误"语法,我不明白。请帮助我,告诉我如何在SQL Server中创建视图:

这是查询:

CREATE VIEW commandefournisseur 
    SELECT  
        F_LOT.LT_IDENT AS LOT_IDENT
        , ISNULL(SUM(LCF_PRIXTOTAL),0) AS PRIX_HT
        , .LCF_LICENCE AS AVENANT
        , LCF_TAUXTVA AS TAUXTVA
        , CF_ETAT AS COTATION
        , FF_CODE_DEVISE AS CODE_DEVISE
        , BPA_NOM_1 AS BATIMENT
        , BPA_ADRESSE_1 AS Adresse
        , ISNULL (SUM(FF_SOLDE),0) as Reste
        , FF_DATE as dateFact
    FROM 
        F_LIGNECOMFOU
    INNER JOIN 
        F_COMMANDEFOU ON CF_NUMERO = LCF_CF_NUMERO
    INNER JOIN 
        F_LOT ON LCF_LT_IDENT = LT_IDENT
    INNER JOIN 
        F_P_FOURNISSEUR ON CF_IDENT_FO = FOU_IDENT
    INNER JOIN 
        F_FACTURESFOU ON FOU_IDENT = FF_IDENT_FO
    INNER JOIN 
        P_PERSONNE ON FOU_BPP_IDENT = BPP_IDENT
    INNER JOIN 
        P_ADRESSE ON BPA_BPP_IDENT = BPP_IDENT
    WHERE 
        LCF_CODE_AFF = '15330-00' 
        AND LT_CODE IN ('S82')
    GROUP BY 
        LT_IDENT, LCF_LICENCE, LCF_TAUXTVA, CF_ETAT, 
        FF_CODE_DEVISE, BPA_NOM_1, BPA_ADRESSE_1, FF_DATE

2 个答案:

答案 0 :(得分:7)

您错过了AS个关键字。

以下是用于在SQL Server中创建视图的标准语法。

SQL Server CREATE VIEW语法

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

在你的情况下

CREATE VIEW commandefournisseur AS
    SELECT  
        F_LOT.LT_IDENT AS LOT_IDENT
        , ISNULL(SUM(LCF_PRIXTOTAL),0) AS PRIX_HT
        , .LCF_LICENCE AS AVENANT
        , LCF_TAUXTVA AS TAUXTVA
        , CF_ETAT AS COTATION
        , FF_CODE_DEVISE AS CODE_DEVISE
        , BPA_NOM_1 AS BATIMENT
        , BPA_ADRESSE_1 AS Adresse
        , ISNULL (SUM(FF_SOLDE),0) as Reste
        , FF_DATE as dateFact
    FROM 
        F_LIGNECOMFOU
    INNER JOIN 
        F_COMMANDEFOU ON CF_NUMERO = LCF_CF_NUMERO
    INNER JOIN 
        F_LOT ON LCF_LT_IDENT = LT_IDENT
    INNER JOIN 
        F_P_FOURNISSEUR ON CF_IDENT_FO = FOU_IDENT
    INNER JOIN 
        F_FACTURESFOU ON FOU_IDENT = FF_IDENT_FO
    INNER JOIN 
        P_PERSONNE ON FOU_BPP_IDENT = BPP_IDENT
    INNER JOIN 
        P_ADRESSE ON BPA_BPP_IDENT = BPP_IDENT
    WHERE 
        LCF_CODE_AFF = '15330-00' 
        AND LT_CODE IN ('S82')
    GROUP BY 
        LT_IDENT, LCF_LICENCE, LCF_TAUXTVA, CF_ETAT, 
        FF_CODE_DEVISE, BPA_NOM_1, BPA_ADRESSE_1, FF_DATE

答案 1 :(得分:1)

您在view_name

之后错过了关键字“AS”
CREATE VIEW commandefournisseur 
AS
SELECT ....

请检查查看语法here