有没有办法阻止SQL Server自动格式化我的视图查询?

时间:2017-01-16 21:30:06

标签: sql-server

拥有通用格式是可以的,我有我的,我和我的团队在编写SQL时使用相同的格式。

现在问题是:我们使用SQL Server。我写了一个长远的观点,然后,为了记住我做了什么,我打开了'#34; design"那个观点。我注意到SQL Server完全搞砸了我的格式,并应用了自己的格式。更糟糕的是,它复制了简单的条件并且无缘无故地使它们更长(也许我有点判断速度......但是......)

这是我的例子,对不起,条件非常长,您不必阅读,只需注意格式:

WHERE                        (STATUT_CAND IN (2, 3)) 
    AND 
                         (DATE_DISPO < GETDATE() OR DATE_DISPO IS NULL) 
    AND
                         ((SELECT        COUNT(ID_CAND) AS Expr1
                             FROM            Paie.dbo.PAI_CAND_RESULT_ENTREVUE AS pcre
                             WHERE        (ID_CAND = PC.ID_CAND)) > 0)
    AND
                         ((SELECT        COUNT(MATR) AS Expr1
                             FROM            Paie.dbo.PAI_DOS_EMPL AS PAI_DOS_EMPL_1
                             WHERE        (MATR = PC.MATR) AND (ETAT = 'C14' OR
                                                      ETAT = 'C15' OR
                                                      ETAT = 'E1') OR
                                                      (MATR = PC.MATR) AND (ETAT LIKE 'A%' OR
                                                      ETAT LIKE 'P%' OR
                                                      ETAT LIKE 'S%') AND (STAT_ENG = 'E1' OR
                                                      STAT_ENG = 'P1' OR
                                                      STAT_ENG = 'G1')) <= 0) 
    AND 
                         (NOT EXISTS
                         (SELECT        ID_CAND
                           FROM            Paie.dbo.PAI_CAND_EMPL AS PAI_CAND_EMPL_1
                           WHERE        (ID_CAND = PC.ID_CAND) AND (MOTIF_EXCLUS IS NOT NULL) AND (MOTIF_EXCLUS NOT IN ('0')))) 
    AND                  (NOT EXISTS
                         (SELECT        MOT_ABS
                           FROM            Paie.dbo.PAI_DOS_PMNT_ABS AS PAI_DOS_PMNT_ABS_1
                           WHERE        (MATR = PC.MATR) AND (MOT_ABS = '15' OR
                                                     MOT_ABS = '16' OR
                                                     MOT_ABS = '40' OR
                                                     MOT_ABS = '41' OR
                                                     MOT_ABS = '45' OR
                                                     MOT_ABS = '46' OR
                                                     MOT_ABS >= '52' AND MOT_ABS <= '57') OR
                                                     (MATR = PC.MATR) AND (MOT_ABS = '01') AND (CODE_PMNT >= 103522 AND CODE_PMNT <= 103541 OR
                                                     CODE_PMNT = 103572 OR
                                                     CODE_PMNT = 103573 OR
                                                     CODE_PMNT = 103577)))
    AND                  (NOT EXISTS
                         (SELECT        STAT_ENG
                           FROM            Paie.dbo.PAI_DOS_EMPL AS pados
                           WHERE        (MATR = PC.MATR) AND (DATE_EFF > GETDATE()))) 
    AND                  (NOT EXISTS
                         (SELECT        MOTIF_AFF
                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS pados
                           WHERE        (PC.MATR = MATR) AND (MOTIF_AFF LIKE '%S%') AND (DATE_EFF =
                                                         (SELECT        MAX(DATE_EFF) AS Expr1
                                                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS aff
                                                           WHERE        (MATR = PC.MATR))))) 


    AND                 ( EXISTS 
                        (SELECT        MATR
                           FROM            Paie.dbo.PAI_HCHQ_PMNT AS dosem
                           WHERE        (MATR = PC.MATR) AND (DATE_FIN > DATEADD (year , -3 , GETDATE() ))) or (select DATE_DERN_PAIE from paie.dbo.PAI_DOS where MATR = PC.MATR) >  DATEADD (year , -3 , GETDATE() ) or (select DATE_DERN_PAIE from paie.dbo.PAI_DOS where MATR = PC.MATR) is null)

这样,我知道MAIN AND在哪里,现在当我把它放到视图中时,这就是SQL Server格式化的方式:

WHERE        (STATUT_CAND IN (2, 3)) AND (DATE_DISPO < GETDATE() OR
                     DATE_DISPO IS NULL) AND
                         ((SELECT        COUNT(ID_CAND) AS Expr1
                             FROM            Paie.dbo.PAI_CAND_RESULT_ENTREVUE AS pcre
                             WHERE        (ID_CAND = PC.ID_CAND)) > 0) AND
                         ((SELECT        COUNT(MATR) AS Expr1
                             FROM            Paie.dbo.PAI_DOS_EMPL AS PAI_DOS_EMPL_1
                             WHERE        (MATR = PC.MATR) AND (ETAT = 'C14' OR
                                                      ETAT = 'C15' OR
                                                      ETAT = 'E1') OR
                                                      (MATR = PC.MATR) AND (ETAT LIKE 'A%' OR
                                                      ETAT LIKE 'P%' OR
                                                      ETAT LIKE 'S%') AND (STAT_ENG = 'E1' OR
                                                      STAT_ENG = 'P1' OR
                                                      STAT_ENG = 'G1')) <= 0) AND (NOT EXISTS
                         (SELECT        ID_CAND
                           FROM            Paie.dbo.PAI_CAND_EMPL AS PAI_CAND_EMPL_1
                           WHERE        (ID_CAND = PC.ID_CAND) AND (MOTIF_EXCLUS IS NOT NULL) AND (MOTIF_EXCLUS NOT IN ('0')))) AND (NOT EXISTS
                         (SELECT        MOT_ABS
                           FROM            Paie.dbo.PAI_DOS_PMNT_ABS AS PAI_DOS_PMNT_ABS_1
                           WHERE        (MATR = PC.MATR) AND (MOT_ABS = '15' OR
                                                     MOT_ABS = '16' OR
                                                     MOT_ABS = '40' OR
                                                     MOT_ABS = '41' OR
                                                     MOT_ABS = '45' OR
                                                     MOT_ABS = '46' OR
                                                     MOT_ABS >= '52' AND MOT_ABS <= '57') OR
                                                     (MATR = PC.MATR) AND (MOT_ABS = '01') AND (CODE_PMNT >= 103522 AND CODE_PMNT <= 103541 OR
                                                     CODE_PMNT = 103572 OR
                                                     CODE_PMNT = 103573 OR
                                                     CODE_PMNT = 103577))) AND (NOT EXISTS
                         (SELECT        STAT_ENG
                           FROM            Paie.dbo.PAI_DOS_EMPL AS pados
                           WHERE        (MATR = PC.MATR) AND (DATE_EFF > GETDATE()))) AND (NOT EXISTS
                         (SELECT        MOTIF_AFF
                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS pados
                           WHERE        (PC.MATR = MATR) AND (MOTIF_AFF LIKE '%S%') AND (DATE_EFF =
                                                         (SELECT        MAX(DATE_EFF) AS Expr1
                                                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS aff
                                                           WHERE        (MATR = PC.MATR))))) AND EXISTS
                         (SELECT        MATR
                           FROM            Paie.dbo.PAI_HCHQ_PMNT AS dosem
                           WHERE        (MATR = PC.MATR) AND (DATE_FIN > DATEADD(year, - 3, GETDATE()))) OR
                     (STATUT_CAND IN (2, 3)) AND (DATE_DISPO < GETDATE() OR
                     DATE_DISPO IS NULL) AND
                         ((SELECT        COUNT(ID_CAND) AS Expr1
                             FROM            Paie.dbo.PAI_CAND_RESULT_ENTREVUE AS pcre
                             WHERE        (ID_CAND = PC.ID_CAND)) > 0) AND
                         ((SELECT        COUNT(MATR) AS Expr1
                             FROM            Paie.dbo.PAI_DOS_EMPL AS PAI_DOS_EMPL_1
                             WHERE        (MATR = PC.MATR) AND (ETAT = 'C14' OR
                                                      ETAT = 'C15' OR
                                                      ETAT = 'E1') OR
                                                      (MATR = PC.MATR) AND (ETAT LIKE 'A%' OR
                                                      ETAT LIKE 'P%' OR
                                                      ETAT LIKE 'S%') AND (STAT_ENG = 'E1' OR
                                                      STAT_ENG = 'P1' OR
                                                      STAT_ENG = 'G1')) <= 0) AND (NOT EXISTS
                         (SELECT        ID_CAND
                           FROM            Paie.dbo.PAI_CAND_EMPL AS PAI_CAND_EMPL_1
                           WHERE        (ID_CAND = PC.ID_CAND) AND (MOTIF_EXCLUS IS NOT NULL) AND (MOTIF_EXCLUS NOT IN ('0')))) AND (NOT EXISTS
                         (SELECT        MOT_ABS
                           FROM            Paie.dbo.PAI_DOS_PMNT_ABS AS PAI_DOS_PMNT_ABS_1
                           WHERE        (MATR = PC.MATR) AND (MOT_ABS = '15' OR
                                                     MOT_ABS = '16' OR
                                                     MOT_ABS = '40' OR
                                                     MOT_ABS = '41' OR
                                                     MOT_ABS = '45' OR
                                                     MOT_ABS = '46' OR
                                                     MOT_ABS >= '52' AND MOT_ABS <= '57') OR
                                                     (MATR = PC.MATR) AND (MOT_ABS = '01') AND (CODE_PMNT >= 103522 AND CODE_PMNT <= 103541 OR
                                                     CODE_PMNT = 103572 OR
                                                     CODE_PMNT = 103573 OR
                                                     CODE_PMNT = 103577))) AND (NOT EXISTS
                         (SELECT        STAT_ENG
                           FROM            Paie.dbo.PAI_DOS_EMPL AS pados
                           WHERE        (MATR = PC.MATR) AND (DATE_EFF > GETDATE()))) AND (NOT EXISTS
                         (SELECT        MOTIF_AFF
                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS pados
                           WHERE        (PC.MATR = MATR) AND (MOTIF_AFF LIKE '%S%') AND (DATE_EFF =
                                                         (SELECT        MAX(DATE_EFF) AS Expr1
                                                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS aff
                                                           WHERE        (MATR = PC.MATR))))) AND
                         ((SELECT        DATE_DERN_PAIE
                             FROM            Paie.dbo.PAI_DOS
                             WHERE        (MATR = PC.MATR)) > DATEADD(year, - 3, GETDATE())) OR
                     (STATUT_CAND IN (2, 3)) AND (DATE_DISPO < GETDATE() OR
                     DATE_DISPO IS NULL) AND
                         ((SELECT        COUNT(ID_CAND) AS Expr1
                             FROM            Paie.dbo.PAI_CAND_RESULT_ENTREVUE AS pcre
                             WHERE        (ID_CAND = PC.ID_CAND)) > 0) AND
                         ((SELECT        COUNT(MATR) AS Expr1
                             FROM            Paie.dbo.PAI_DOS_EMPL AS PAI_DOS_EMPL_1
                             WHERE        (MATR = PC.MATR) AND (ETAT = 'C14' OR
                                                      ETAT = 'C15' OR
                                                      ETAT = 'E1') OR
                                                      (MATR = PC.MATR) AND (ETAT LIKE 'A%' OR
                                                      ETAT LIKE 'P%' OR
                                                      ETAT LIKE 'S%') AND (STAT_ENG = 'E1' OR
                                                      STAT_ENG = 'P1' OR
                                                      STAT_ENG = 'G1')) <= 0) AND (NOT EXISTS
                         (SELECT        ID_CAND
                           FROM            Paie.dbo.PAI_CAND_EMPL AS PAI_CAND_EMPL_1
                           WHERE        (ID_CAND = PC.ID_CAND) AND (MOTIF_EXCLUS IS NOT NULL) AND (MOTIF_EXCLUS NOT IN ('0')))) AND (NOT EXISTS
                         (SELECT        MOT_ABS
                           FROM            Paie.dbo.PAI_DOS_PMNT_ABS AS PAI_DOS_PMNT_ABS_1
                           WHERE        (MATR = PC.MATR) AND (MOT_ABS = '15' OR
                                                     MOT_ABS = '16' OR
                                                     MOT_ABS = '40' OR
                                                     MOT_ABS = '41' OR
                                                     MOT_ABS = '45' OR
                                                     MOT_ABS = '46' OR
                                                     MOT_ABS >= '52' AND MOT_ABS <= '57') OR
                                                     (MATR = PC.MATR) AND (MOT_ABS = '01') AND (CODE_PMNT >= 103522 AND CODE_PMNT <= 103541 OR
                                                     CODE_PMNT = 103572 OR
                                                     CODE_PMNT = 103573 OR
                                                     CODE_PMNT = 103577))) AND (NOT EXISTS
                         (SELECT        STAT_ENG
                           FROM            Paie.dbo.PAI_DOS_EMPL AS pados
                           WHERE        (MATR = PC.MATR) AND (DATE_EFF > GETDATE()))) AND (NOT EXISTS
                         (SELECT        MOTIF_AFF
                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS pados
                           WHERE        (PC.MATR = MATR) AND (MOTIF_AFF LIKE '%S%') AND (DATE_EFF =
                                                         (SELECT        MAX(DATE_EFF) AS Expr1
                                                           FROM            Paie.dbo.GRH_POSTE_AFFECT AS aff
                                                           WHERE        (MATR = PC.MATR))))) AND
                         ((SELECT        DATE_DERN_PAIE
                             FROM            Paie.dbo.PAI_DOS AS PAI_DOS_1

不仅几乎不可能理解任何事情(个人而言,我不会)但它比我强大的时间长3倍 ......不可接受,有没有办法转向自动格式化?如果不是,我会将格式保存在单独的文件中,并且每次都复制粘贴。

1 个答案:

答案 0 :(得分:4)

不要使用设计师。格式化确实很糟糕。只需右键单击&#34;脚本作为创建到新窗口&#34;。它将保持您的格式。正如Aaron Bertrand评论的那样,你应该避免使用任何的视觉设计师。