TaoK / PoorMansTSqlFormatter

A small free .Net and JS library (with demo UI, command-line bulk formatter, SSMS/VS add-in, notepad++ plugin, winmerge plugin, and demo webpage) for reformatting and coloring T-SQL code to the user's preferences.
http://www.architectshack.com/PoorMansTSqlFormatter.ashx
GNU Affero General Public License v3.0
957 stars 266 forks source link

"IF EXISTS" qualifier mistaken for "IF" statement #203

Open llzenoll opened 6 years ago

llzenoll commented 6 years ago

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 ( ....
MizardX commented 5 years ago

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 ]
mojolama commented 5 years ago

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

data-architect-tampa commented 4 years ago

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.

dudesam commented 4 years ago

@TaoK hi! any chanse for fix?

cldbrr commented 4 years ago

Are there any plans to fix it

erapade commented 3 years ago

Agree. DROP VIEW IF EXISTS viewName doesn't work

JDeeth commented 2 weeks ago

This is my workaround:

--[noformat]
DROP TABLE IF EXISTS Students;
IF 1 = 0 SELECT 1; --[/noformat]

which is I think the minimal amount of boilerplate to allow the rest of the query to autoformat correctly