For example, in MS-SQL, you can open up a query window and run the following:
DECLARE @List AS VARCHAR(8)
SELECT @List = 'foobar'
SELECT *
FROM dbo.PubLists
WHERE Name = @List
How is this done in PostgreSQL? Can it be done?
For example, in MS-SQL, you can open up a query window and run the following:
DECLARE @List AS VARCHAR(8)
SELECT @List = 'foobar'
SELECT *
FROM dbo.PubLists
WHERE Name = @List
How is this done in PostgreSQL? Can it be done?
You can use:
That will do
I've came across some other documents which they use
\set
to declare scripting variable but the value is seems to be like constant value and I'm finding for way that can be acts like a variable not a constant variable.Ex:
Here
sal
is the value that is present in the table 'emp' andcomm
is the constant value.Complete answer is located in the official PostgreSQL documentation.
You can use new PG9.0 anonymous code block feature (http://www.postgresql.org/docs/9.1/static/sql-do.html )
Also you can get the last insert id:
Postgresql does not have bare variables, you could use a temporary table. variables are only available in code blocks or as a user-interface feature.
If you need a bare variable you could use a temporary table:
Building on @nad2000's answer and @Pavel's answer here, this is where I ended up for my Flyway migration scripts. Handling for scenarios where the database schema was manually modified.
I had to do something like this