I have the following code in a .sql file that I run against my postgresql database:
INSERT INTO widget (username)
SELECT i
FROM generate_series(100, 150) AS t(i);
INSERT INTO widget2
SELECT generate_series(100, 150), 'statictext', generate_series(100, 150);
And I run it by doing something like "psql -U myuser -d widgets -f addwidgets.sql But now I need to parameterize these items. So I've created this version instead:
DECLARE startnum INTEGER;
DECLARE endnum INTEGER;
startnum:=100;
endnum:=150;
INSERT INTO widget (username)
SELECT i
FROM generate_series(startnum, endnum) AS t(i);
INSERT INTO widget2
SELECT generate_series(startnum, endnum), 'statictext', generate_series(startnum, endnum);
But it bombs because it doesn't like my startnum / endnum variables. The error message is:
psql:addwidgets.sql:1: ERROR: syntax error at or near "INTEGER"
LINE 1: DECLARE startnum INTEGER;
In searching for an answer, I found that it could be related to the language I'm using in psql. Right now i'm googling how to find out what scripting language my database engine is set up to understand. But in the mean time if you have any suggestions I'd appreciate it.
EDIT 1
So I found this:
testbox:/tmp# psql -U postgres -d widgets -c "select * from pg_language;"
lanname | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl
----------+----------+---------+--------------+---------------+-----------+--------------+--------
internal | 10 | f | f | 0 | 0 | 2246 |
c | 10 | f | f | 0 | 0 | 2247 |
sql | 10 | f | t | 0 | 0 | 2248 |
plpgsql | 10 | t | t | 11864 | 11865 | 11866 |
Perhaps I should specify at the top of my .sql file what language I'm using? it would also help me to know which documentation to reference I guess. :)
DOblock: postgresql.org/docs/current/static/sql-do.html