inferrinizzard / prettier-sql

[ARCHIVED] Please use https://github.com/sql-formatter-org/sql-formatter
https://github.com/sql-formatter-org/sql-formatter
MIT License
21 stars 5 forks source link

[VSCODE] Error reading properties of undefined #82

Closed RyanGrange closed 2 years ago

RyanGrange commented 2 years ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Open a .sql script file
  2. Press ALT+SHIFT+f
  3. See error

Expected behavior SQL formatted

Error Unable to format SQL: TypeError: Cannot read properties of undefined (reading 'value') Source: Prettier SQL VSCode (Extension)

Usage

VS Code version: 1.66.2 Extension version: 0.3.0 Settings: { "[sql]": { "editor.defaultFormatter": "inferrinizzard.prettier-sql-vscode" } "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.detectIndentation": true, "files.insertFinalNewline": true, "files.trimFinalNewlines": true, "files.trimTrailingWhitespace": true, "files.eol": "\n" }

inferrinizzard commented 2 years ago

Can you provide the query that you trying to format ?

RyanGrange commented 2 years ago

(Sorry for all the edits you're probably getting emailed. I missed the preview tab initially.) Any of our stored procedures I try to format with the formatter set to inferrinizzard.prettier-sql-vscode pops up the notification... Unable to format SQL: TypeError: Cannot read properties of undefined (reading 'value')

Source: Prettier SQL VSCode (Extension)

Here's an example with company specific names removed.

USE [TestDB]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'TestProc')
BEGIN
    exec('CREATE PROCEDURE [dbo].[TestProc] AS BEGIN SET NOCOUNT ON END;');
    IF SUSER_NAME() != 'testuser' BEGIN
        GRANT EXECUTE ON dbo.TestProc TO testuser;
    END
END
GO

ALTER PROCEDURE [dbo].[TestProc]
AS BEGIN
    SET NOCOUNT ON;

    declare @batchsize int = 5000
        ,@timestamp varchar(33)
        ,@totalrows int = 0
        ,@rowcnt int = 0;

    select @rowcnt = 1, @totalrows = 0;
    while (@rowcnt > 0)
    begin
        delete top (@batchsize) testalias
        from dbo.TestTable testalias
        where testalias.TestDtField < dateadd(day, -31, getutcdate())

        set @rowcnt = @@ROWCOUNT;
        set @totalrows = @totalrows + @rowcnt;
        waitfor delay '00:00:00.5'
    end
    set @timestamp = convert(varchar(33), getutcdate(), 126);
    raiserror ('%s: %i entries deleted', 0, 1, @timestamp, @totalrows) with nowait
END
GO

It did, however, work with a very simple SQL statement:

select * from dbo.TestTable where TestTable.TestDTField > dateadd(day, -7, getutcdate());

->

SELECT *
FROM dbo.TestTable
WHERE
    TestTable.TestDTField > dateadd(DAY, -7, getutcdate());
RyanGrange commented 2 years ago

Oh. Crap. I just saw on the home page/readme that it doesn't support stored procedures. I'll give some others a try. Sorry to waste your time on this.