pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedvariables in pgSQL?
3+ messages / 2 participants
[nested] [flat]
* variables in pgSQL?
@ 1999-07-13 02:18 Heinz Hemken <zotz@franchiseloan.com>
0 siblings, 0 replies; 3+ messages in thread
From: Heinz Hemken @ 1999-07-13 02:18 UTC (permalink / raw)
To: pgsql-sql
The following is legal in MS SQL:
declare @user_id int
select @user_id = user_id from masterindex_table where masterindexid = 1
whereupon the local variable can be used for other things. Is there
anything similar in pgSQL? I want to have a user table for a web
database, and I want to be able to do a begin/commit block where I 1)
check to see if the user exists in an account table, 2) if not, get the
next available userid from a master index table, 3) add a new userid to
the account table for the new user, and 4) increment the userid field in
the master index table. I want this to occur all within a single
transaction block so that no race conditions exist, and the masterindex
table is only being used by one process at a time for the whole
operation.
How can I do this in postgresql? Is there a better approach?
--
Heinz Hemken
IFC Capital Corporation
San Diego, California
^ permalink raw reply [nested|flat] 3+ messages in thread
* variables in pgSQL?
@ 1999-07-13 02:41 Heinz Hemken <zotz@franchiseloan.com>
0 siblings, 1 reply; 3+ messages in thread
From: Heinz Hemken @ 1999-07-13 02:41 UTC (permalink / raw)
To: pgsql-sql
The following is legal in MS SQL:
declare @user_id int
select @user_id = user_id from masterindex_table where masterindexid = 1
whereupon the local variable can be used for other things. Is there
anything similar in pgSQL? I want to have a user table for a web
database, and I want to be able to do a begin/commit block where I 1)
check to see if the user exists in an account table, 2) if not, get the
next available userid from a master index table, 3) add a new userid to
the account table for the new user, and 4) increment the userid field in
the master index table. I want this to occur all within a single
transaction block so that no race conditions exist, and the masterindex
table is only being used by one process at a time for the whole
operation.
How can I do this in postgresql? Is there a better approach?
--
Heinz Hemken
IFC Capital Corporation
San Diego, California
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: [SQL] variables in pgSQL?
@ 1999-07-13 03:12 Chris Bitmead <chris@tech.com.au>
parent: Heinz Hemken <zotz@franchiseloan.com>
0 siblings, 0 replies; 3+ messages in thread
From: Chris Bitmead @ 1999-07-13 03:12 UTC (permalink / raw)
To: Heinz Hemken <zotz@franchiseloan.com>; +Cc: pgsql-sql
CREATE SEQUENCE id_seq;
INSERT INTO masterindex_table(user_id, name) SELECT nextval('id_seq'),
'Mr Foo' from masterindex_table where 12345 not in (SELECT user_id from
masterindex_table);
This will create a new record in masterindex_table only if the id 12345
is not already existing. All in one SQL command!
Heinz Hemken wrote:
>
> The following is legal in MS SQL:
>
> declare @user_id int
> select @user_id = user_id from masterindex_table where masterindexid = 1
>
> whereupon the local variable can be used for other things. Is there
> anything similar in pgSQL? I want to have a user table for a web
> database, and I want to be able to do a begin/commit block where I 1)
> check to see if the user exists in an account table, 2) if not, get the
> next available userid from a master index table, 3) add a new userid to
> the account table for the new user, and 4) increment the userid field in
> the master index table. I want this to occur all within a single
> transaction block so that no race conditions exist, and the masterindex
> table is only being used by one process at a time for the whole
> operation.
>
> How can I do this in postgresql? Is there a better approach?
>
> --
> Heinz Hemken
> IFC Capital Corporation
> San Diego, C
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~1999-07-13 03:12 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-07-13 02:18 variables in pgSQL? Heinz Hemken <zotz@franchiseloan.com>
1999-07-13 02:41 variables in pgSQL? Heinz Hemken <zotz@franchiseloan.com>
1999-07-13 03:12 ` Chris Bitmead <chris@tech.com.au>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox