title: Declaring temporary variables in PostgreSQL
body: <p>I'm migrating from SQL Server to PostgreSQL. I've seen from <a href="https://stackoverflow.com/questions/1490942/how-to-declare-a-variable-in-a-postgresql-query">How to declare a variable in a PostgreSQL query</a> that there is no such thing as temporary variables in native sql queries.</p>
  
  <p>Well, I pretty badly need a few... How would I go about mixing in <code>plpgsql</code>? Must I create a function and then delete the function in order to get access to a language? that just seems error prone to me and I'm afraid I'm missing something.</p>
  
  <p>EDIT:</p>
  
  <pre><code>cmd.CommandText="insert......" +
  "declare @app int; declare @gid int;"+
  "set @app=SCOPE_IDENTITY();"+ //select scope_identity will give us our RID that we just inserted
  "select @gid=MAX(GROUPID) from HOUSEHOLD; set @gid=@gid+1; "+
  "insert into HOUSEHOLD (APPLICANT_RID,GROUPID,ISHOH) values "+
  "(@app,@gid,1);"+
  "select @app";
  rid=cmd.ExecuteScalar();
  </code></pre>
  
  <p>A direct rip from the application in which it's used. Note we are in the process of converting from SQL server to Postgre. (also, I've figured out the <code>scope_identity()</code> bit I think)</p>