How would i implement this code in postgresql using psql?
Declare @Id int
Set @Id = 1
While @Id <= 100
Begin
Insert Into tblAuthors values ('Author - ' + CAST(@Id as nvarchar(10)),
'Country - ' + CAST(@Id as nvarchar(10)) + ' name')
Print @Id
Set @Id = @Id + 1
End
I’ve tried to search some tutorials for postgresql syntax but none of them works.
2
Answers
No need for PL/pgSQL, a loop or variables:
The answer by @a_horse_with_no_name is correct, a single insert is all that is required. However, your question seems more about syntax rather than how to accomplish. So let’s review; some observations necessary to convert your code to Postgres.
plpgsql
notpsql
.Psql
in the command line interface application intoPostgresql
database management system.@
is not a valid character in an object name.Set
not used for variable assignment. If used it establishes/changes the run-time environmentnvarchar
data type. Just usetext
.Print
statement. Useraise notice
instead.+
is not used for string concatenation. Use either the functionconcat()
or the operator (||
).;
).loop ... end loop;
begin ... end;
.Dollar-Quoted String Constant.
Inserts. (IMHO is should be required).
Accounting for the above your code becomes:
Instead of a search some tutorials a good place to start would be the Documentation. More specifically perhaps the chapter Lexical Structure
NOTE: #2 above is not strictly true. You can us
@
in a variable name if it is enclosed in double quotes as"@name"
. However, double quotes become a read pia very quickly and are best avoided completely.