-1

I am trying to run this simple WHILE loop in pgAdmin's query tool:

DECLARE @counter INT = 1;

WHILE @counter <= 5
BEGIN
    PRINT @counter;
    SET @counter = @counter + 1;
END

The desired result is just a list from 1 to 5, but my pgAdmin is returning this error: *ERROR: syntax error at or near "@" LINE 1: DECLARE @counter INT = 1;

I'm an undergrad in a non-IT course, so I hope you consider this in your explanations. Thank you! (code source)

Andy
  • 21
  • 5

2 Answers2

1

Your example is from SQL Server, not PostgreSQL. You should try something like this:

do $$
declare 
   counter integer := 0;
begin
   while counter < 5 loop
      raise notice 'Counter %', counter;
      counter := counter + 1;
   end loop;
end$$;
Oleksii Tambovtsev
  • 2,666
  • 1
  • 3
  • 21
  • Ah, thank you so much for clearing this up! I appreciate it :) Is there a way to be able to just print the counter without having it output "NOTICE"? – Andy Dec 29 '21 at 16:39
  • @deanne: why not simply use `select * from generate_series(0,5)` –  Dec 29 '21 at 17:18
0

This answer assumed the source was postgres syntax, not sql-server, due to the incorrect tag of the question.

SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($).

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245