PoorMansTSqlFormatter icon indicating copy to clipboard operation
PoorMansTSqlFormatter copied to clipboard

"IF EXISTS" qualifier mistaken for "IF" statement

Open llzenoll opened this issue 7 years ago • 7 comments

The tool doesnt recognize drop view if exists as a key word. This issue also causes poor identation for the rest of the document

INPUT

use views;
drop view if exists important_view;
create view important_view as
--Requested by: user
--Created by: user2
--Target DB: db

 with view1
    as ( ....

OUTPUT

use views;
drop view 
if exists important_view;
    create view important_view as
    --Requested by: user
    --Created by: user2
    --Target DB: db

    with view1
        as ( ....

llzenoll avatar Jul 04 '18 15:07 llzenoll

Same for other statements:

DROP AGGREGATE [ IF EXISTS ]
DROP ASSEMBLY [ IF EXISTS ]
DROP DATABASE [ IF EXISTS ]
DROP DEFAULT [ IF EXISTS ]
DROP FUNCTION [ IF EXISTS ]
DROP INDEX [ IF EXISTS ]
DROP PROC [ IF EXISTS ]
DROP PROCEDURE [ IF EXISTS ]
DROP ROLE [ IF EXISTS ]
DROP RULE [ IF EXISTS ]
DROP SCHEMA [ IF EXISTS ]
DROP SECURITY POLICY [ IF EXISTS ]
DROP SEQUENCE [ IF EXISTS ]
DROP SYNONYM [ IF EXISTS ]
DROP TABLE [ IF EXISTS ]
DROP TRIGGER [ IF EXISTS ]
DROP TYPE [ IF EXISTS ]
DROP USER [ IF EXISTS ]
DROP VIEW [ IF EXISTS ]

MizardX avatar Dec 03 '18 13:12 MizardX

I would rename this issue "IF EXISTS" qualifier mistaken for "IF" statement

mojolama avatar Jun 13 '19 15:06 mojolama

This is really annoying because I have to use the older syntax in order to avoid formatting issues: IF object_id('tempdb..#negs') IS NOT NULL DROP TABLE #negs;

instead of:

DROP TABLE if exists #negs;

I tried using -- [noformat] , -- [\noformat] and -- [minify] ,-- [\minify] tags and they failed to recognize the end tags and made the formatting worse.

The parsing key here might be noting that EXISTS does not have a parenthesis expression after it. therefore it must be the IF EXISTS keyword. We need to differentiate between :

IF Exists ( select * from foo) .... and the drop commands.

data-architect-tampa avatar Nov 14 '19 15:11 data-architect-tampa

@TaoK hi! any chanse for fix?

dudesam avatar Jan 17 '20 13:01 dudesam

Are there any plans to fix it

cldbrr avatar Jun 25 '20 09:06 cldbrr

Agree. DROP VIEW IF EXISTS viewName doesn't work

erapade avatar Nov 19 '20 08:11 erapade