views:

730

answers:

6

Hello, I'm migrating from SQL Server to PostgreSQL. I've seen from http://stackoverflow.com/questions/1490942/how-to-declare-a-variable-in-a-postgresql-query that there is no such thing as temporary variables in native sql queries.

Well, I pretty badly need a few... How would I go about mixing in plpgsql? 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.

EDIT:

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();

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 scope_identity() bit I think)

+1  A: 

You can also declare session variables using plperl - http://www.postgresql.org/docs/8.4/static/plperl-global.html

Alex Brasetvik
A: 

I think that PostgreSQL's row-type variable would be the closest thing:

A variable of a composite type is called a row variable (or row-type variable). Such a variable can hold a whole row of a SELECT or FOR query result, so long as that query's column set matches the declared type of the variable.

Andrew Hare
A: 

If you're using a language binding, you can hold the variables there.

For example with SQLAlchemy (python):

my_var = 'Reynardine'
session.query(User.name).filter(User.fullname==my_var)

If you're in psql, you have variables:

\set a 5
SELECT :a;

And if your logic is in PL/pgSQL:

tax := subtotal * 0.06;
Tobu
I am wanting to do these queries from my own program, not from psql
Earlz
Thanks. What language binding are you using?
Tobu
plpgsql.. I'm not sure how to use it freestanding though... (like in a sql query without creating a function)
Earlz
I was thinking of a library like SQLAlchemy, psycopg2 or ActiveRecord.
Tobu
@earlz: You cannot. Procedural languages are for functions/procedures only.
Alex Brasetvik
A: 

you install a language that you want to use with the CREATE LANGUAGE command for known languages. Although you can use other languages.

Language installation docs

CREATE LANGUAGE usage doc

You will have to create a function to use it. If you do not want to make a permanent function in the db then the other choice would be to use a scrip in python or something that uses a postgresql driver to connect to the db and do queries. You can then manipulate or look through the data in the script. For instance in python you would install the pygresql library and in your script import pgdb which you can use to connect to the db.

PyGreSQL Info

Arthur Thomas
+1  A: 

Must I create a function and then delete the function in order to get access to a language?

Yes, but this shortcoming is going to be removed in PostgreSQL 8.5, with the addition of DO command. 8.5 is going to be released in 2010.

filiprem
+3  A: 

What is your schema for the table being inserted? I'll try and answer based on this assumption of the schema:

CREATE TABLE HOUSEHOLD (
    APPLICANT_RID SERIAL,  -- PostgreSQL auto-increment
    GROUPID INTEGER,
    ISHOH INTEGER
);

If I'm understanding your intent correctly, in PostgreSQL >= 8.2, the query would then be:

INSERT INTO HOUSEHOLD (GROUPID, ISHOH)
VALUES ((SELECT COALESCE(MAX(GROUPID)+1,1) FROM HOUSEHOLD), 1)
RETURNING APPLICANT_RID;

-- Added call to the COALESCE function to cover the case where HOUSEHOLD 
-- is empty and MAX(GROUPID) returns NULL

In PostgreSQL >= 8.2, any INSERT/DELETE/UPDATE query may have a RETURNING clause that acts like a simple SELECT performed on the result set of the change query.

Matthew Wood
I was not aware you could nest selects inside of inserts and such.. I guess I just need to read up more on Ansi SQL(if that is standard anyway)
Earlz