MS SQL Semicolon on declaring variables
See original GitHub issueHi, Fist at all, great work with DBeaver. Love it as I can connect to multiple servers and types of databases from one single place.
However, I have found a problem. If I have this:
DECLARE @sc_CountryOfExport nvarchar(13)='United States';
SELECT * FROM dbo.Country WHERE [Name] = @sc_CountryOfExport;
And execute the script (not execute the statement) I get an error that I need to declare scalar @sc_countryOfExport
.
If I remove the semicolon then it works OK.
The semicolon is a ‘statement terminator’ but I don’t think that means that the next statement gets a different context (like the keyword GO
).
The reason why I say this is because in the Microsoft tools, both forms (with and without semicolon) work exactly the same.
However in Microsoft tools, if I insert a GO
in between the 2 lines, then it behaves like in DBeaver.
As per the documentation, GO
is a “batch” terminator (https://msdn.microsoft.com/en-us/library/ms188037.aspx)
So it seems like DBeaver is treating a statement terminator as a batch terminator. This could be very problematic, because very soon semicolon will be mandatory and MS tools will behave differently (we’ll have to go back to those, instead of using awesome DBeaver) https://msdn.microsoft.com/en-us/library/ms177563(v=sql.110).aspx
Notice that DECLARE
is considered a statement and it serves to declare variables in the “batch”. So DECLARE
will have a semicolon and DBeaver will not work under this scenario.
https://msdn.microsoft.com/en-us/library/ms188927(v=sql.110).aspx
(The example in the above page does not run DBeaver due to the semicolon in the DECLARE
lines)
I’m using DBeaver 3.8.0
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:12 (1 by maintainers)
Top GitHub Comments
Yes, SQL Server is special.
When you execute in script mode (alt+x) then DBeaver splits script text on statements (using
;
andGO
) and then executes statements one by one. In opposite to MySQL each statement is executed as a batch and variables context is lost after every statement execution. For DBeaver there is no difference between;
andGO
in script mode - they are just delimiters.If you need to execute your script as a single batch - use query execution (CTRL+Enter). You can select whole script (CTRL+A) and then run it.
So far I don’t see how I can improve this.
Edit 2: In Window >> Preferences >> Database >> Editors >> SQL Editor >> SQL Processing, under the “Delimiters” section
Now the only statement delimiter left is the “native” delimiter, which for MSSQL is “GO”
I believe this resolves the issue, as well as my observation below about the
merge
command. Note that depending on how many different database technologies you work with, you may have to keep this preferences window open and modify the settings depending on what DB you’re querying…As a consequence of the way DBeaver splits script text on statements (using ; and GO) and then executes statements one by one (per Serge’s comment above on 2017-03-25), the only way to execute a
merge
command on MSSQL is to highlight it and hit CTRL-Enter.Sample Test Script:
If you run the script (ALT-X) you get
because evidently the text between but not including the semi-colon is executed.
This is still true as of version 6.2.5.
A (probably easy) fix for the
merge
problem is to append a semi-colon to each block of text that DBeaver executes. MSSQL commands will always work with a semi-colon terminator, but sometimes not work without one.But the larger issue could be fixed if DBeaver keeps its current script parsing logic, except for MSSQL, use only
go
(on a line by itself) as the delimiter between blocks of text to execute, ignoring semicolons completely. Then DBeaver would treat MSSQL scripts the same way as SSMS, Azure Data Studio. SQLCMD, etc.