I'm trying to populate my database table with some dummy data and I wrote a loop for that. I'm using DBeaver to connect to my SQL Server db and I run the code against it.
DECLARE @counter INT = 1;
WHILE @counter <= 1000
BEGIN
INSERT INTO dbo.Articles
(ArticleCode, ArticleName)
VALUES
('ARTCODE' + CAST(@counter AS VARCHAR), 'Name' + CAST(@counter AS VARCHAR))
;
SET @counter = @counter + 1;
END;
For some reason, I get an error on line 3 (on the WHILE loop).
SQL Error [137] [S0002]: Must declare the scalar variable "@counter"
What did I do wrong?
The problem is DBeaver, rather than your SQL. DBeaver, when used with T-SQL, sees a semicolon (
;) as a batch separator not a statement terminator, despite the fact that it's defined as a "statement terminator" in the settings.As a result of this undesired behaviour (bug) this means that even a simple batch like the following will fail:
In SSMS, it would be like writing the following:
(2
GOs on purpose as DBeaver also treats a blank line as astatementbatch separator.)Instead, you'll need to change your preferences in Editors>SQL Editor>SQL Processing. The method I used to get normal T-SQL behaviour was:
GOThis meant the above script worked and an error in a second batch that
@testwasn't defined, and the on-screen "intellisense" appeared to give correct behaviour too. Screenshot