question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MS SQL Semicolon on declaring variables

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
serge-ridercommented, Mar 25, 2017

Yes, SQL Server is special.

When you execute in script mode (alt+x) then DBeaver splits script text on statements (using ; and GO) 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 ; and GO 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.

3reactions
jetzerbcommented, Nov 20, 2019

Edit 2: In Window >> Preferences >> Database >> Editors >> SQL Editor >> SQL Processing, under the “Delimiters” section image

  1. Clear the “Statements delimiter” text box
  2. Uncheck the “Ignore native delimiter” box
  3. Uncheck the “Blank line is statement delimiter” box

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:

drop table if exists #test;
create table #test (id int, name varchar(10));

merge into #test tgt
using (values
	 (1,'Alice')
	,(2,'Bob')
) src(id,name)
on tgt.id = src.id
when matched then
	update
	set name = src.name
when not matched then
	insert (    id,    name)
	values (src.id,src.name)
; -- <<--- Note the presence of the semi-colon

If you run the script (ALT-X) you get

Error occurred during SQL query execution

Reason: SQL Error [10713] [S0001]: A MERGE statement must be terminated by a semi-colon (😉.

because evidently the text between but not including the semi-colon is executed.

Error? Highlighting Scenario
Yes None Run script (ALT-X)
Yes Entire Script Run script (ALT-X)
Yes None Run statement (CTRL-Enter) when cursor is positioned within the statement
No Entire Statement Run statement (CTRL-Enter)
No Entire Script Run statement (CTRL-Enter)–But this runs the entire script (all highlighted statements)

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Semicolon Usage Examples - MSSQLTips.com
The semicolon (;) is used in SQL code as a statement terminator. For most SQL Server T-SQL statements it is not mandatory.
Read more >
When should I use semicolons in SQL Server? - Stack Overflow
SQL Server requires the semicolon only in particular cases—but in cases where a semicolon is not required, using one doesn't cause problems. I ......
Read more >
Fundamentals: The semicolon (;) is a statement terminator
This means that should you have SQL that isn't properly terminated, and Microsoft do (eventually) remove the functionality of not terminating ...
Read more >
syntax for @var in column containing comma separated values
If your SQL server is 2016 or higher, try this: DECLARE @T TABLE (; rec_id int,; [desc] varchar(20),; stores varchar(20); );; INSERT INTO...
Read more >
The GO Command and the Semicolon Terminator
One of the new syntax requirements for SQL Server 2005 is the use of semicolons in certain situations. Many T-SQL programmers are unfamiliar ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found