diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.sqlserverKeywords.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.sqlserverKeywords.spec.ts new file mode 100644 index 0000000000..c68bb5d8c6 --- /dev/null +++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.sqlserverKeywords.spec.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from "vitest"; +import { buildSqlCompletionItems } from "@/lib/sql/sqlCompletion"; + +describe("SQL Server T-SQL keyword completion", () => { + function labelsFor(prefix: string): string[] { + return buildSqlCompletionItems(prefix, prefix.length, { + databaseType: "sqlserver", + tables: [], + columnsByTable: new Map(), + }).map((item) => item.label); + } + + it("offers DECLARE ahead of DECIMAL for the decl prefix (issue #9319)", () => { + const labels = labelsFor("decl"); + + expect(labels).toContain("DECLARE"); + expect(labels).toContain("DECIMAL"); + expect(labels.indexOf("DECLARE")).toBeLessThan(labels.indexOf("DECIMAL")); + }); + + it.each(["declare", "exec", "print", "raiserror", "waitfor", "throw", "isnull", "getdate", "newid", "scope_identity", "error_message"])("offers %s for a SQL Server batch", (keyword) => { + expect(labelsFor(keyword.slice(0, -1))).toContain(keyword.toUpperCase()); + }); +}); diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts index 97fe30b44b..743eba22a5 100644 --- a/apps/desktop/src/lib/sql/sqlCompletion.ts +++ b/apps/desktop/src/lib/sql/sqlCompletion.ts @@ -534,6 +534,73 @@ const SQLSERVER_SQL_KEYWORDS = [ "SHOWPLAN_ALL", "SHOWPLAN_TEXT", "SHOWPLAN_XML", + // T-SQL statements and control flow. `DECLARE` in particular is what issue #9319 reported: + // typing `decl` only offered `DECIMAL`, so scripting a batch had no completion at all. + "DECLARE", + "EXEC", + "EXECUTE", + "PRINT", + "RETURN", + "IF", + "WHILE", + "BREAK", + "CONTINUE", + "GOTO", + "WAITFOR", + "RAISERROR", + "THROW", + "TRY", + "CATCH", + "BEGIN TRY", + "BEGIN CATCH", + "BEGIN TRANSACTION", + "COMMIT TRANSACTION", + "ROLLBACK TRANSACTION", + "SAVE TRANSACTION", + "TRIGGER", + "PROCEDURE", + "FUNCTION", + "RETURNS", + "PIVOT", + "UNPIVOT", + "CROSS APPLY", + "OUTER APPLY", + "OPTION", + "RECOMPILE", + "MAXRECURSION", + // Frequently used T-SQL functions and session/system values. Built-in functions with + // parameter signatures (CONVERT, ISNULL, GETDATE, NEWID, STUFF, ...) are completed from + // SQLSERVER_FUNCTION_SIGNATURES instead; buildKeywordItems filters keywords that already + // have a signature, so they are not duplicated here. IIF has no SQL Server signature + // entry and is therefore listed as a keyword. + "IIF", + "CHOOSE", + "STRING_AGG", + "OBJECT_ID", + "SCOPE_IDENTITY", + "IDENT_CURRENT", + "SP_EXECUTESQL", + "SYSTEM_USER", + "SESSION_USER", + "CURRENT_USER", + "USER_NAME", + "SCHEMA_NAME", + "DATABASEPROPERTYEX", + "XACT_STATE", + "ERROR_MESSAGE", + "ERROR_NUMBER", + "ERROR_SEVERITY", + "ERROR_STATE", + "ERROR_LINE", + "ERROR_PROCEDURE", + "@@ERROR", + "@@IDENTITY", + "@@ROWCOUNT", + "@@TRANCOUNT", + "@@FETCH_STATUS", + "@@SERVERNAME", + "@@VERSION", + "@@SPID", ]; function sqlDialectCompletionWords(...sources: Array): string[] { diff --git a/docs/screenshots/issue-9319-sqlserver-declare-completion.png b/docs/screenshots/issue-9319-sqlserver-declare-completion.png new file mode 100644 index 0000000000..c4ba3b9c57 Binary files /dev/null and b/docs/screenshots/issue-9319-sqlserver-declare-completion.png differ